Python Lists: The Go-To Data Structure for Collections
Almost every real program deals with groups of things — a shopping cart full of items, a leaderboard of player scores, a to-do list of tasks. Python's list is the built-in tool for storing ordered collections of data. Once you understand lists, you can write programs that handle real-world data at any scale.
Lists are mutable (you can add, remove, and change items), ordered (items stay in the order you put them), and flexible (they can hold any mix of data types). They're used in everything from web APIs to machine learning datasets.
Creating a Python List
Wrap your items in square brackets [], separated by commas. That's it:
# A list of strings
cities = ["New York", "London", "Tokyo"]
# A list of numbers
scores = [98, 74, 85, 91]
# A mixed list (any types work)
profile = ["Alex", 28, True, 4.9]
# An empty list (to fill later)
cart = []
print(cities) # ['New York', 'London', 'Tokyo']
print(scores) # [98, 74, 85, 91]
Accessing Items by Index
Python lists are zero-indexed — the first item is at position 0, the second at 1, and so on. Use square bracket notation to access any item:
cities = ["New York", "London", "Tokyo"]
print(cities[0]) # New York (first item)
print(cities[1]) # London (second item)
print(cities[2]) # Tokyo (third item)
# Negative indexing counts from the end:
print(cities[-1]) # Tokyo (last item)
print(cities[-2]) # London (second to last)
Modifying a List
Unlike strings, lists are mutable — you can change, add, and remove items after creation:
scores = [98, 74, 85]
# Change an item:
scores[1] = 80
print(scores) # [98, 80, 85]
# Add to the end:
scores.append(91)
print(scores) # [98, 80, 85, 91]
# Remove by value:
scores.remove(80)
print(scores) # [98, 85, 91]
# Remove by index:
scores.pop(0)
print(scores) # [85, 91]
Real-World Example: Managing a Task List
tasks = ["Write report", "Send email", "Review code"]
# Mark task done by removing it:
tasks.remove("Send email")
# Add a new task:
tasks.append("Update docs")
# How many tasks remain?
print("Tasks left:", len(tasks)) # Tasks left: 3
print(tasks)
# ['Write report', 'Review code', 'Update docs']
Useful List Operations at a Glance
len(my_list)— number of items in the listmy_list.append(x)— addxto the endmy_list.insert(i, x)— insertxat indeximy_list.remove(x)— remove the first occurrence ofxmy_list.pop(i)— remove and return item at indexix in my_list— check ifxexists in the list (returnsTrue/False)my_list.sort()— sort the list in place
Common Mistakes to Avoid
- IndexError — accessing out-of-range positions: A list with 3 items only has indices 0, 1, and 2. Trying
list[3]crashes. - Confusing
append()withextend():append([1, 2])adds one nested list item;extend([1, 2])adds two separate items. - Modifying a list while iterating over it: This can skip items or cause errors. Iterate over a copy instead.
nums = [1, 2, 3]
# IndexError:
# print(nums[5]) # IndexError: list index out of range
# append vs extend:
a = [1, 2]
a.append([3, 4])
print(a) # [1, 2, [3, 4]] — one nested list
b = [1, 2]
b.extend([3, 4])
print(b) # [1, 2, 3, 4] — four separate items
Key Takeaways
- A Python list stores an ordered collection of items in square brackets:
my_list = [a, b, c]. - Access items with their index:
my_list[0]for the first,my_list[-1]for the last. - Lists are mutable — use
append(),remove(), andpop()to modify them. - Use
len()to check how many items a list contains.
What to Learn Next
With lists in your toolkit, the next step is learning how to access individual elements by index and how to loop through a list with a for loop. These two skills let you process every item in a collection — the basis of data processing in Python.
