Table of contents

list

  • List as a Pre-defined Class: In Python, 'list' is a pre-defined class and is categorized as a List data type.
  • Purpose of List: Lists are used to store multiple values, which can be of the same type, different types, or a mix of both, including unique and duplicate values, within a single object.
# creating list with different vales or heterogeneous data var = [1, True, "Hello", 23.5, 3+4j] print(var,type(var))
  • Syntax for Lists: List elements must be enclosed within square brackets [ ], separated by commas. Examples include: Non-empty list: varname = [val1, val2, ..., val-n]Empty list: varname = [] or varname = list()
# Creating an empty list var = [] print(var,type(var))
# creating an empty list v = list() print(a)
# creating non non-empty list var = [1,2,3] print(var,type(var))
  • Insertion Order: A list object maintains the insertion order of elements, meaning elements are stored and retrieved in the order they were added.
  • Indexing and Slicing: Lists support both indexing (accessing individual elements) and slicing (accessing a range of elements) operations.
  • Mutability: List objects are mutable, allowing modifications such as adding, removing, or updating elements after the list is created.