Lists: Your First Collection
Lesson 1 of 6 · View course roadmap
Learn the idea
A list stores an ordered collection of items — any types, any length, changeable at any time:
fruits = ["apple", "banana", "cherry"]fruits[0]→"apple"(index from 0),fruits[-1]→ last itemfruits.append("kiwi")— add to the endlen(fruits)— how many items"apple" in fruits→True— membership test
Loop over a list directly — no indexes needed: for fruit in fruits:. This is the Pythonic way.
Where you'll use this
API responses arrive as lists of records; every table, feed and search result you render starts life as a list. append() inside a loop is the fundamental 'collect results' pattern of data processing.
Common mistakes
IndexError: list index out of range— a 4-item list has indexes 0–3, and an empty list has none at all.- Copy confusion:
b = amakes both names point to the same list; changing one changes 'both'. Real copy:b = a.copy()orb = a[:]. append()vsextend(): append([4,5]) adds one nested list; extend([4,5]) adds two numbers.
Pro tip
Lists have a fast membership test but for thousands of 'x in collection' checks, convert to a set first — it's hundreds of times faster on large data.
Watch how it runs — line by line
Press play to watch Python execute this code, one line at a time.
Try it yourself
The lesson example is loaded and ready — press Run, then change something and run it again. Breaking it is part of learning. Want a clean slate? Tap “New blank”.
Output appears here…
Pass the challenge +20 XP
Create a list langs containing "Python", "SQL" and "JavaScript". Append "Rust", then print the list's length and its last item.
4 Rust
print(len(langs)) then print(langs[-1]).
langs = ["Python", "SQL", "JavaScript"]
langs.append("Rust")
print(len(langs))
print(langs[-1])
Run your code to check it…
Check your understanding
Your notes (saved on this device)
Tip: use ← and → to move between lessons, ⌘K to search everything.