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.
8 questions · take it digitally for instant feedback at tendril.neural-forge.io/learn/quiz/end-prog-python-lists-dicts-builders
What is the main idea of "Python Lists & Dicts — The Two Collections You Can't Live Without"?
Which concept is most central to "Python Lists & Dicts — The Two Collections You Can't Live Without"?
Which use of AI fits this topic best?
What should a careful learner remember about "Mutation aliasing"?
You want to use AI after this lesson. What is the safest next step?
How should AI output about list be treated?
Name one way to verify an AI answer about list.
Which action would help you apply "Python Lists & Dicts — The Two Collections You Can't Live Without" responsibly?