Loading lesson…
Lists are ordered rows; dicts are labeled lookups. You'll use both to solve a real problem, and catch the mistakes autocomplete makes.
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.
# 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 defaultAlways use .get() when a key might be missing — it returns None (or a default) instead of crashing.# 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}Comprehensions read left-to-right: 'the result, for each thing, if some condition.'| 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 |
# 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)]A pattern you'll use forever: dict as counter, then sorted by value.Big idea: most real programs are lists of dicts or dicts of lists. Master these two and 80% of Python feels natural.
15 questions · take it digitally for instant feedback at tendril.neural-forge.io/learn/quiz/end-prog-python-lists-dicts-builders
What is the core idea behind "Python Lists & Dicts — The Two Collections You Can't Live Without"?
Which term best describes a foundational idea in "Python Lists & Dicts — The Two Collections You Can't Live Without"?
A learner studying Python Lists & Dicts — The Two Collections You Can't Live Without would need to understand which concept?
Which of these is directly relevant to Python Lists & Dicts — The Two Collections You Can't Live Without?
Which of the following is a key point about Python Lists & Dicts — The Two Collections You Can't Live Without?
Which of these does NOT belong in a discussion of Python Lists & Dicts — The Two Collections You Can't Live Without?
What is the key insight about "Mutation aliasing" in the context of Python Lists & Dicts — The Two Collections You Can't Live Without?
What is the recommended tip about "Review before you run" in the context of Python Lists & Dicts — The Two Collections You Can't Live Without?
Which statement accurately describes an aspect of Python Lists & Dicts — The Two Collections You Can't Live Without?
What does working with Python Lists & Dicts — The Two Collections You Can't Live Without typically involve?
Which best describes the scope of "Python Lists & Dicts — The Two Collections You Can't Live Without"?
Which section heading best belongs in a lesson about Python Lists & Dicts — The Two Collections You Can't Live Without?
Which section heading best belongs in a lesson about Python Lists & Dicts — The Two Collections You Can't Live Without?
Which of the following is a concept covered in Python Lists & Dicts — The Two Collections You Can't Live Without?
Which of the following is a concept covered in Python Lists & Dicts — The Two Collections You Can't Live Without?