A list is a collection data type in Python used to store multiple items in a single variable.
Lists are ordered, changeable (mutable), and allow duplicate values.
Lists are one of the most commonly used data structures in Python.
CREATING A LIST
Lists are created using square brackets [ ].
numbers = [1, 2, 3, 4, 5]
Lists can store different data types:
mixed = [10, "Python", 3.14, True]
ACCESSING LIST ELEMENTS
List items are accessed using indexing.
Indexing starts from 0.
fruits = ["Apple", "Banana", "Mango"]print(fruits[0]) # Apple
print(fruits[1]) # Banana
Negative indexing is also allowed:
print(fruits[-1]) # Mango
SLICING A LIST
Slicing allows you to get a range of elements.
numbers = [1, 2, 3, 4, 5]print(numbers[1:4]) # [2, 3, 4]
MODIFYING LIST ELEMENTS
Lists are mutable, meaning you can change their values.
fruits = ["Apple", "Banana", "Mango"]
fruits[1] = "Orange"print(fruits)
ADDING ELEMENTS TO A LIST
append() β Adds item at the end
numbers = [1, 2, 3]
numbers.append(4)
insert() β Adds item at specific position
numbers.insert(1, 10)
extend() β Adds multiple items
numbers.extend([5, 6])
REMOVING ELEMENTS FROM A LIST
remove() β Removes specific value
numbers.remove(10)
pop() β Removes item by index
numbers.pop(0)
clear() β Removes all items
numbers.clear()
LOOPING THROUGH A LIST
fruits = ["Apple", "Banana", "Mango"]for fruit in fruits:
print(fruit)
COMMON LIST FUNCTIONS
numbers = [5, 2, 8, 1]print(len(numbers)) # Length
print(max(numbers)) # Maximum value
print(min(numbers)) # Minimum valuenumbers.sort() # Sort list
numbers.reverse() # Reverse list
LIST COMPREHENSION
A shorter way to create lists.
squares = [x * x for x in range(5)]
print(squares)
IMPORTANT POINTS
β’ Lists are ordered and indexed
β’ Lists are mutable (can be changed)
β’ Allow duplicate values
β’ Can store multiple data types
WHY LISTS ARE IMPORTANT
β’ Store multiple values efficiently
β’ Useful in data processing
β’ Essential for loops and functions
β’ Widely used in real-world Python applications
Understanding lists is fundamental for working with data in Python.