05
Day 5: Lists and List Methods
Chapter 5 • Beginner
40 min
Lists are used to store multiple items in a single variable. Lists are one of 4 built-in data types in Python used to store collections of data.
List Properties:
- Ordered (items have a defined order)
- Changeable (can add, remove, modify items)
- Allow duplicate values
- Indexed (can access items by index)
- Can contain different data types
Common List Methods:
- append() - Add item to end
- insert() - Insert item at position
- remove() - Remove first occurrence
- pop() - Remove item at index
- clear() - Remove all items
- index() - Find index of item
- count() - Count occurrences
- sort() - Sort the list
- reverse() - Reverse the list
Hands-on Examples
List Operations
# Create a list
fruits = ["apple", "banana", "cherry"]
numbers = [1, 2, 3, 4, 5]
mixed = ["hello", 42, True, 3.14]
print("Fruits:", fruits)
print("Numbers:", numbers)
print("Mixed:", mixed)
# Access elements
print("First fruit:", fruits[0])
print("Last fruit:", fruits[-1])
print("Slice:", fruits[1:3])
# Modify list
fruits.append("orange")
fruits.insert(1, "grape")
print("After modifications:", fruits)
# List methods
print("Length:", len(fruits))
print("Index of banana:", fruits.index("banana"))
print("Count of apple:", fruits.count("apple"))Lists are mutable, meaning you can change their content after creation.