Python Programming · Unit 4 of 8
Unit 4: Dictionaries, sets and comprehensions
Dictionaries for lookups by key, counting and grouping records, Counter and defaultdict, sets for uniqueness and fast membership, choosing a structure, and comprehensions.
Opens the first lesson of this unit in Python Programming. Every lesson is open from the start.
Lesson 1: Dictionaries
Goal: Store and look up values by key, update and remove entries, and loop over keys, values and items.
A dictionary (type dict) maps keys to values, like a real dictionary maps words to definitions or a contact list maps names to phone numbers. You write it in braces with key: value pairs: stock = {"pens": 12, "ink": 0, "pads": 7}. Instead of asking "what is at position 2?", you ask "what is the value for pens?": stock["pens"] is 12. Looking up a key is very fast however big the dictionary gets.
| Code | Effect |
|---|---|
stock["pens"] | the value for a key; raises KeyError if the key is missing |
stock.get("tape") | the value, or None if missing (no error) |
stock.get("tape", 0) | the value, or the default you give |
stock["tape"] = 5 | adds a new key, or replaces the value of an existing one |
del stock["ink"] / stock.pop("ink") | removes a key (pop also returns its value) |
"pens" in stock | True if the key exists (checks keys, not values) |
len(stock) | the number of keys |
Keys must be unique and immutable: strings, numbers and tuples work, lists do not (you would get TypeError: unhashable type). Values can be anything, including lists and other dictionaries. Since Python 3.7, dictionaries remember the order in which keys were first added, so looping over one is predictable.
stock.keys(), stock.values() and stock.items() let you loop over the keys, the values, or (key, value) pairs. Looping over the dictionary itself gives the keys. The most common loop by far unpacks items: for name, qty in stock.items():.
stock = {"pens": 12, "ink": 0, "pads": 7}
stock["tape"] = 5 # a new key
stock["pens"] -= 2 # update an existing value
for name, qty in stock.items():
status = "REORDER" if qty < 3 else "ok"
print(f"{name:<5} {qty:>3} {status}")
print("Glue in stock:", stock.get("glue", 0))
Worked example: restocking report
- Adding "tape" and updating "pens" both use square-bracket assignment.
items()gives each (key, value) pair in the order the keys were added.- A conditional expression picks the status for each line.
get("glue", 0)returns the default 0 instead of raising KeyError for a missing key.
Four aligned lines with ink marked REORDER, then Glue in stock: 0.
Hands-on lab in the room: Python playground.
Hands-on lab in the room: Unit 4 coding exercises. Graded exercise: look up prices and collect the unknown items.
- Dictionary (dict)
- A collection that maps unique keys to values, written {key: value}.
- Key
- The name you look a value up by in a dictionary; it must be immutable, such as a string or number.
- KeyError
- The error raised when you read a dictionary key that does not exist.
- get()
- A dictionary method that returns the value for a key, or a default instead of raising KeyError.
- items()
- A dictionary method giving (key, value) pairs, usually unpacked in a for loop.
Remember: Look values up by key: d[key] raises KeyError when it is missing, d.get(key, default) does not. Loop with for k, v in d.items().
Lesson 2: Counting, grouping and nested data
Goal: Count and group with dictionaries, work with records as lists of dictionaries, and use Counter and defaultdict.
Two dictionary patterns appear in almost every data task. Counting: one key per thing, and the value is how many times you have seen it. Grouping: one key per group, and the value is a list of the items in that group. Both start with an empty dictionary and fill it in a loop.
orders = [
{"id": 1, "city": "Lakeside", "total": 40},
{"id": 2, "city": "Riverton", "total": 15},
{"id": 3, "city": "Lakeside", "total": 22},
]
counts = {}
by_city = {}
for order in orders:
city = order["city"]
counts[city] = counts.get(city, 0) + 1 # counting
by_city.setdefault(city, []).append(order["id"]) # grouping
print(counts) # {'Lakeside': 2, 'Riverton': 1}
print(by_city) # {'Lakeside': [1, 3], 'Riverton': [2]}
Notice the data itself: a list of dictionaries, where each dictionary is one record with named fields. This is the shape you get from JSON APIs, CSV files read with csv.DictReader and database queries, so get comfortable with order["city"] inside a loop. Nested data is read from the outside in: data["customers"][0]["name"] means "the customers list, its first item, that item's name".
The collections module, part of Python's standard library, has shortcuts for both patterns. A Counter is a dictionary that counts, starting every key at 0, and most_common(n) gives the top n. A defaultdict(list) creates an empty list the first time you use a new key, so grouping becomes one line. You import them with from collections import Counter, defaultdict.
from collections import Counter, defaultdict
cities = ["Lakeside", "Riverton", "Lakeside", "Hillcrest", "Lakeside"]
counts = Counter(cities)
print(counts.most_common(2))
totals = defaultdict(float)
for city, amount in [("Lakeside", 40), ("Riverton", 15), ("Lakeside", 22)]:
totals[city] += amount
print(dict(totals))
Worked example: the same report with collections
Counter(cities)counts the whole list in one call.most_common(2)returns the two biggest counts as (key, count) tuples:[('Lakeside', 3), ('Riverton', 1)](ties keep first-seen order).defaultdict(float)starts each new city at 0.0, so+=works without checking first.dict(totals)turns it back into a plain dictionary for printing.
Know both versions: interviewers often ask for the plain-dictionary one, and real code often uses collections.
Hands-on lab in the room: Python playground.
Hands-on lab in the room: Unit 4 coding exercises. Graded exercise: the counting pattern on real text.
- Record
- One item of structured data with named fields, often a dictionary such as {"id": 1, "city": "Lakeside"}.
- Counting pattern
- counts[key] = counts.get(key, 0) + 1 inside a loop.
- Grouping pattern
- Collecting items into a list per key, for example with setdefault(key, []).append(item).
- Counter
- A dictionary from the collections module that counts things and offers most_common().
- defaultdict
- A dictionary from the collections module that creates a default value (such as [] or 0) for new keys.
Remember: Count with counts[k] = counts.get(k, 0) + 1; group with setdefault(k, []).append(x). Counter and defaultdict do the same in less code.
Lesson 3: Sets and choosing a structure
Goal: Use sets for unique values, fast membership and comparisons, and pick the right structure for a job.
A set is an unordered collection of unique values: tags = {"python", "data", "web"}. Adding a value that is already there changes nothing, so turning a list into a set is the quickest way to remove duplicates: set(["a", "b", "a"]) is {"a", "b"}. Sets have no order and no indexes, so tags[0] is an error. An empty set is written set(), because {} is an empty dictionary.
Sets shine at two jobs. Membership: "python" in tags is very fast even with millions of items, while in on a list checks items one by one. Comparisons: the operators below answer questions about two groups directly.
| Operator | Meaning | Question it answers |
|---|---|---|
a | b | union | Who is in either group? |
a & b | intersection | Who is in both groups? |
a - b | difference | Who is in a but not in b? |
a ^ b | symmetric difference | Who is in exactly one of the groups? |
october = {"ana", "ben", "cy", "dee"}
november = {"ben", "dee", "eli"}
print(sorted(october & november)) # came both months
print(sorted(november - october)) # new in November
print(sorted(october - november)) # did not come back
print(len(october | november)) # different customers overall
Worked example: which customers came back?
&keeps names in both sets: ben and dee.november - octoberkeeps names only in November: eli.october - novemberkeeps the customers who did not return: ana and cy.|combines both, counting each person once: 5 different customers.
Each line answers a real business question with one operator. sorted() turns each set into a list in a fixed order for printing.
Choosing the right structure is a core skill: it often decides whether code is simple or tangled, and fast or slow.
| Use a... | when you need... | Example |
|---|---|---|
| list | an ordered sequence that can change, with duplicates allowed | the lines of a receipt |
| tuple | a fixed bundle of values with set meanings | (latitude, longitude) |
| dict | to look values up by a key | price of each product |
| set | unique values and fast "is it in there?" | customers who have ordered |
Hands-on lab in the room: Unit 4 coding exercises. Graded exercise: compare two months of customers with sets.
- Set
- An unordered collection of unique values, written {a, b} (empty: set()).
- Membership test
- Checking whether a value is in a collection with in; fast for sets and dict keys.
- Union / intersection
- All items in either set (|) / only the items in both sets (&).
- Difference
- The items in one set that are not in another (a - b).
Remember: Sets keep one of each value and answer "is it in there?" fast. & is both, | is either, - is only in the first.
Lesson 4: Comprehensions
Goal: Write list, dictionary and set comprehensions with filters, and know when a plain loop is clearer.
A comprehension builds a list, dictionary or set from another collection in a single expression. It is the transform and filter loop patterns written compactly. Compare:
prices = [4.0, 12.5, 3.0, 20.0]
# the loop version
with_tax = []
for p in prices:
with_tax.append(round(p * 1.2, 2))
# the comprehension version: [expression for item in collection]
with_tax = [round(p * 1.2, 2) for p in prices]
# with a filter: [expression for item in collection if condition]
cheap = [p for p in prices if p < 10]
Read a comprehension from the middle: "for each p in prices, if the condition holds, give me the expression". Square brackets make a list. Braces with key: value make a dictionary comprehension, such as {name: len(name) for name in names}. Braces with a single value make a set comprehension, such as {word.lower() for word in words}. With round brackets inside a function call you get a generator expression, which feeds values one at a time without building a list: sum(p * q for p, q in lines).
stock = {"pens": 12, "ink": 0, "pads": 7, "tape": 2}
low = [name for name, qty in stock.items() if qty < 5]
doubled = {name: qty * 2 for name, qty in stock.items()}
first_letters = {name[0] for name in stock}
print(low)
print(doubled)
print(sorted(first_letters))
Worked example: three comprehensions on one dataset
lowis a filter: the names whose quantity is under 5, giving['ink', 'tape'].doubledis a dictionary comprehension that keeps every key and doubles each value.first_lettersis a set comprehension, so the two names starting with p give one p.- Each one replaces a three- or four-line loop.
Output: ['ink', 'tape'], the doubled dictionary, then ['i', 'p', 't'].
When not to: comprehensions are for building a collection. If the logic needs several steps, nested conditions or side effects such as printing, a plain loop is clearer. A good rule: if it does not fit comfortably on one line, or a colleague would have to read it twice, write the loop.
Hands-on lab in the room: Python playground.
Hands-on lab in the room: Unit 4 coding exercises. Graded exercise: write one of each kind of comprehension.
- List comprehension
- An expression [expr for item in items if condition] that builds a new list.
- Dictionary comprehension
- An expression {key: value for item in items} that builds a new dictionary.
- Set comprehension
- An expression {expr for item in items} that builds a new set.
- Generator expression
- A comprehension in round brackets that produces values one at a time, often passed to sum(), max() or any().
Remember: Read comprehensions from the middle: for each item, if it matches, give the expression. If it is hard to read, write the loop.
Practise it in Python Programming
Each lesson ends with three questions that check you can use the idea, not just recognise it. Wrong answers explain why and come back in Review. The unit ends with a 7-question quiz; pass it with 80% or more to master the unit. Hands-on lab: Unit 4 coding exercises.
Every lesson is free: no ads, no lives and no paywall.