Introduction to Python Lists
From the PYTHON curriculum
TL;DR
Python lists are ordered collections of items that can hold different data types. You can change lists after they're created, making them very flexible for storing and managing data. They're fundamental for organizing information in your Python programs.
1. The Mental Model
Imagine a Python list as a shopping list. You can write down different things (like "apples", 3, "milk"), in a specific order, and you can always add more items, remove items, or change an item on the list later.
2. The Core Material
Lists are one of the most versatile data structures in Python. They allow you to store multiple items in a single variable.
2.1 What is a List?

Photo by cottonbro studio on Pexels
A list is:
* Ordered: The items have a defined order, and that order won't change unless you modify the list.
* Changeable (Mutable): You can add, remove, or change items in a list after it's created.
* Allows Duplicates: You can have the same item appear multiple times in a list.
* Holds Various Data Types: A single list can contain numbers, strings, Booleans, and even other lists!
You create a list by placing all the items inside square brackets [], separated by commas.
my_first_list = ["apple", "banana", "cherry"]
numbers = [1, 2, 3, 4, 5]
mixed_list = ["hello", 10, True, 3.14]
empty_list = []
2.2 Accessing List Items (Indexing)

Photo by Tima Miroshnichenko on Pexels
Each item in a list has an index, which is its position. Python uses zero-based indexing, meaning the first item is at index 0, the second at 1, and so on.
fruits = ["apple", "banana", "cherry", "date"]
print(fruits[0]) # Output: apple (the first item)
print(fruits[2]) # Output: cherry (the third item)
You can also use negative indexing:
* -1 refers to the last item.
* -2 refers to the second to last item.
print(fruits[-1]) # Output: date (the last item)
print(fruits[-3]) # Output: banana (the second item)
2.3 Modifying Lists

Photo by Veronika Andrews on Pexels
Since lists are changeable, you can easily modify them.
Changing an item:
Assign a new value to an item using its index.
fruits = ["apple", "banana", "cherry"]
fruits[1] = "orange" # Change "banana" to "orange"
print(fruits) # Output: ['apple', 'orange', 'cherry']
Adding items:
append(): Adds an item to the end of the list.insert(): Adds an item at a specified index.
fruits.append("grape")
print(fruits) # Output: ['apple', 'orange', 'cherry', 'grape']
fruits.insert(1, "kiwi") # Insert "kiwi" at index 1
print(fruits) # Output: ['apple', 'kiwi', 'orange', 'cherry', 'grape']
Removing items:
remove(): Removes the first occurrence of a specified value.pop(): Removes the item at a specified index. If no index is given, it removes and returns the last item.del: Removes an item at a specified index or deletes the entire list.
fruits.remove("orange")
print(fruits) # Output: ['apple', 'kiwi', 'cherry', 'grape']
last_fruit = fruits.pop() # Removes and returns 'grape'
print(fruits) # Output: ['apple', 'kiwi', 'cherry']
print(last_fruit) # Output: grape
del fruits[0] # Deletes "apple"
print(fruits) # Output: ['kiwi', 'cherry']
# del fruits # This would delete the entire list 'fruits'
2.4 List Operations

Photo by Syirwan Ainu on Pexels
Here's how various list operations fit together:
graph TD
A["Create List []"] --> B{Want to change items?};
B -- Yes --> C["Access item by index: my_list[idx]"];
B -- Yes --> D["Modify: my_list[idx] = new_value"];
B -- Yes --> E{Want to add items?};
E -- Yes --> F["Add to end: my_list.append(item)"];
E -- Yes --> G["Add at specific spot: my_list.insert(idx, item)"];
B -- Yes --> H{Want to remove items?};
H -- Yes --> I["Remove by value: my_list.remove(item)"];
H -- Yes --> J["Remove by index: my_list.pop(idx)"];
H -- Yes --> K["Remove by index/slice: del my_list[idx]"];
B -- No --> L["Access item by index: my_list[idx]"];
L --> M["Iterate through list: for item in my_list:"];
3. Worked Example
Let's say you're building a simple inventory for a small shop.
-
Initialize your inventory: Start with some items.
python shop_inventory = ["T-Shirt", "Jeans", "Socks", "Hat"] print("Initial Inventory:", shop_inventory) # Output: Initial Inventory: ['T-Shirt', 'Jeans', 'Socks', 'Hat'] -
Add a new item that just arrived:
python shop_inventory.append("Scarf") print("After adding Scarf:", shop_inventory) # Output: After adding Scarf: ['T-Shirt', 'Jeans', 'Socks', 'Hat', 'Scarf'] -
A customer bought the Hat, so remove it:
python shop_inventory.remove("Hat") print("After selling Hat:", shop_inventory) # Output: After selling Hat: ['T-Shirt', 'Jeans', 'Socks', 'Scarf'] -
Oops, the Socks were mislabeled; change them to Gloves:
python shop_inventory[2] = "Gloves" # Socks was at index 2 print("After correcting Socks to Gloves:", shop_inventory) # Output: After correcting Socks to Gloves: ['T-Shirt', 'Jeans', 'Gloves', 'Scarf'] -
Check what the first item in your inventory is:
python print("First item in inventory:", shop_inventory[0]) # Output: First item in inventory: T-Shirt
4. Key Takeaways
- Lists are ordered collections of items enclosed in square brackets
[]. - Items in a list are accessed using zero-based indexing (the first item is at index
0). - Lists are mutable, meaning you can change, add, or remove items after creation.
- Use
append()to add an item to the end andinsert(index, item)to add at a specific position. - Use
remove(value)to remove by item value orpop(index)to remove by index. - Lists can store items of different data types (e.g., numbers, strings, Booleans) in the same list.
Common Mistakes to Avoid
- Forgetting zero-based indexing: Always remember that the first item is at index 0, not 1.
- Index out of range: Trying to access an index that doesn't exist (e.g.,
my_list[10]for a list with only 5 items). - Modifying while iterating: If you're looping through a list and adding/removing items from that same list, it can lead to unexpected behavior.
- Confusing
append()withextend():append()adds a single item,extend()adds elements from another iterable (like another list).
5. Now Try It
Create a list called my_to_do_list with at least three tasks. Then, add a new task to the end, mark the second task as completed by changing its text (e.g., from "Buy groceries" to "Buy groceries - DONE"), and finally, remove the very first task from the list. Print the list at each step to see the changes.
Frequently asked about Introduction to Python Lists
Study this next
Get the full PYTHON curriculum
Clone the complete plan to your dashboard for unlimited AI-generated notes, practice quizzes, and a personalised revision schedule.
Save this course free