Learning outcomes
By the end of this lesson you should be able to: build and index a list; add to a list and loop over it; build a dictionary and look values up by key; and use the dictionary get() + 1 idiom to count occurrences — the pattern that lies at the heart of the PY4E course.
Prerequisites
Lessons 4, 5 and 7. You need conditions, loops, and the idea of a function — because a dictionary usually lives inside a loop you are iterating, and the counting pattern you will learn is the whole reason these structures exist.
The big idea
Until now, every variable you wrote held exactly one value. That is a real constraint: to store a list of names, you would have needed one variable per name. A list lets you hold many values in one box, ordered and addressable. A dictionary lets you hold many values but address them by a meaningful label rather than a number. These are the first two structures that let a program work with real collections of data — the raw material of everything from inventory to word counts.
Why this matters before the syntax
Choose the structure by the question you need to ask. If you need to know the position, or to keep things in a defined order, use a list. If you need to look up a value by a label — a name, a word, an id — use a dictionary. Trying to use a dictionary as an ordered list, or a list as a lookup table, is the most common awkwardness. A dictionary has no meaningful 'third position' because it is ordered by insertion but not indexed that way; and a list has no fast 'find the value for Ada' mechanism because it has no keys.
The second thing is that these structures are genuinely useful only when combined with a loop. A list is built to be iterated: for item in my_list. A dictionary is built to be used inside a loop where you accumulate something keyed by a label. The powerful pattern is the two together: a dictionary initialized before a loop, updated on each pass, keyed by whatever the loop encounters.
The single most valuable idiom in the whole course is the counting pattern: counts[key] = counts.get(key, 0) + 1. It does three things at once: it asks the dictionary what count is currently there, with a default of 0 if the key has never been seen; it adds one to that; and it stores the result back under the same key. One line, and it turns a stream of data into a tally. You will use this repeatedly from now through the final project.
Worked example 1: lists — build and index
A list is created with square brackets and indexed from zero. The index is a position, not a rank you count from one.
fruits = ["apple", "banana", "cherry"]
print(len(fruits))
print(fruits[0])
print(fruits[1])3
apple
bananaThe hard part for most beginners is the zero-based indexing. fruits[0] is the first element (apple), not the second. This is why loops that count through a list often go from 0 to len minus one, or why range(len(...)) looks one short. Once you accept that the first item lives at index 0, list indexing becomes predictable.
Worked example 2: adding to a list and looping
append grows a list in place, and a for loop visits each element. Two ways to see a list grow and be consumed.
fruits = ["apple", "banana", "cherry"]
fruits.append("mango")
print(len(fruits))
for f in fruits:
print(f)4
apple
banana
cherry
mangoappend modifies the list in place and does not return a new one, which is why you write fruits.append(...) as a statement rather than assigning. The for loop is the natural way to consume a list — it visits each element in order, one per pass. This pairing — a list built by appending, then consumed by a for loop — is the backbone of collecting results.
Worked example 3: dictionaries — look up by key and update
A dictionary matches keys to values and is built with braces. Look up by key; set by assigning to that key.
ages = {"Ada": 19, "Gustav": 34}
print(ages["Ada"])
ages["Ada"] = 20
print(ages["Ada"])19
20The key is the label, not a number. You ask for Ada and get 19. Assigning ages["Ada"] = 20 replaces the value 19 with 20 under the same key. There is no 'first entry' or 'second entry' to worry about — you address a dictionary purely by its keys. If you asked for a key that does not exist, ages["Mina"], Python would raise a KeyError, which is where the get() method in the next example comes in.
Worked example 4: the counting idiom
Count how often each word appears. The dictionary tracks the tally, and a single line does the whole job per word.
counts = {}
for w in ["the", "cat", "the", "dog"]:
counts[w] = counts.get(w, 0) + 1
print(counts){'the': 2, 'cat': 1, 'dog': 1}Walk the first word, the. counts is empty, so counts.get("the", 0) returns the default 0. Add 1 and store, giving counts["the"] = 1. The second word, cat, is unseen, so it also gets 1. When the appears again, counts.get("the", 0) now returns 1 — the existing tally — add 1 to get 2, and store 2. Reading the statement as "give me what's there, or 0, then add one and put it back" makes it second nature. This one idiom is the engine of the word-count programme you will build at the end of the course.
Common mistakes
Watch outForgetting that list indexing starts at 0. fruits[1] is the second element, not the first. This off-by-one is the single most common list bug.
Watch outLooking up a missing key without a default. ages["Mina"] raises a KeyError if Mina is not there. Use ages.get("Mina", 0) to fall back gracefully.
Watch outTreating a dictionary like an ordered list. A dictionary is keyed by label, not indexed by position, so don't expect a 'third entry'.
Watch outMaking a list rather than a dictionary for a lookup task. To count or to find-by-label, a dictionary is the right structure; a list only offers position.
Check your understanding
What is the index of the first element of a list, and why does it surprise beginners?
Why does ages["Mina"] raise an error while ages.get("Mina", 0) does not?
In the counting idiom counts[w] = counts.get(w, 0) + 1, what does the 0 do?
Multiple choice
Which structure is best for counting how many times each word appears?
What is the index of the first element in a list?
Which method adds an element to the end of a list?
What does counts.get("x", 0) return when "x" is not in counts?
Which best describes a dictionary?
What is the result of counts.get(w, 0) + 1 in the counting idiom?
To read a value from a dictionary and handle a missing key gracefully, which form is correct?
Why are lists and dictionaries best combined with loops?
Summary and next
A list is an ordered, position-addressable collection; a dictionary is a set of key-value pairs looked up by key. Choose the structure by the question you must answer. The counting idiom — counts[key] = counts.get(key, 0) + 1 — is the single most important pattern, and you will reach for it constantly.
Next: strings & files. Lesson 8 gave you structures to hold data; the next lesson shows you how to get text into them — slicing and splitting strings, then reading a file line by line — which closes the loop between stored data and the information you actually want to analyse.