Lesson 148 of 1570
Python Lists & Dicts — The Two Collections You Can't Live Without
Lists are ordered rows; dicts are labeled lookups. You'll use both to solve a real problem, and catch the mistakes autocomplete makes.
Lesson map
What this lesson covers
Learning path
The main moves in order
- 1Two containers, two jobs
- 2list
- 3dict
- 4comprehension
Concept cluster
Terms to connect while reading
Section 1
Two containers, two jobs
A list holds items in order, accessed by index. A dict holds values under keys, like a labeled drawer system. Pick the one that matches how you will look things up later.
Always use .get() when a key might be missing — it returns None (or a default) instead of crashing.
# Lists: ordered, indexed by number
scores = [87, 92, 76, 95]
print(scores[0]) # 87
print(scores[-1]) # 95 (last item)
scores.append(88) # add to the end
print(len(scores)) # 5
# Dicts: accessed by key
student = {
"name": "Ava",
"grade": 8,
"subjects": ["math", "art"],
}
print(student["name"]) # Ava
student["age"] = 13 # add a new key
print(student.get("age", 0))# safe access with defaultList comprehension — Python's superpower
Comprehensions read left-to-right: 'the result, for each thing, if some condition.'
# Without comprehension
doubled = []
for n in [1, 2, 3, 4]:
doubled.append(n * 2)
# With comprehension — one line
doubled = [n * 2 for n in [1, 2, 3, 4]]
# With a filter
evens = [n for n in range(10) if n % 2 == 0]
# Dict comprehension
squares = {n: n*n for n in range(1, 6)}
# {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}Mini-exercise: class roster
- 1Build a list of 4 student dicts, each with name and score
- 2Write a comprehension that returns just the names of students with score >= 80
- 3Write another that returns a dict mapping name -> grade letter (A/B/C/F)
- 4Have your AI assistant draft both — then check if it handled ties and missing keys
Compare the options
| Use a list when... | Use a dict when... |
|---|---|
| Order matters | You look up by key, not position |
| You iterate in order | You need O(1) lookup |
| Example: queue of tasks | Example: user -> profile |
A pattern you'll use forever: dict as counter, then sorted by value.
# Real mini-project: word frequency counter
text = "the cat sat on the mat and the dog sat too"
words = text.split()
freq = {}
for word in words:
freq[word] = freq.get(word, 0) + 1
# Sort by count, descending
top = sorted(freq.items(), key=lambda pair: pair[1], reverse=True)
print(top[:3]) # [('the', 3), ('sat', 2), ('cat', 1)]Key terms in this lesson
Big idea: most real programs are lists of dicts or dicts of lists. Master these two and 80% of Python feels natural.
End-of-lesson quiz
Check what stuck
15 questions · Score saves to your progress.
Tutor
Curious about “Python Lists & Dicts — The Two Collections You Can't Live Without”?
Ask anything about this lesson. I’ll answer using just what you’re reading — short, friendly, grounded.
Progress saved locally in this browser. Sign in to sync across devices.
Related lessons
Keep going
Builders · 30 min
Python Lists and Dicts With AI
Lists hold ordered items. Dicts hold keyed pairs. Comprehensions make both sing. Learn the core patterns AI will push you toward.
Builders · 25 min
What Does AI-Assisted Coding Even Mean?
AI-assisted coding is not magic and not cheating. It is a new way of working where a model drafts, you decide. Let's draw a map before we start building.
Builders · 30 min
Your First Copilot-Style Completion
Let's actually feel what autocomplete is like. Write a comment, pause, and watch a full function appear. Then learn what to do next.
