BeginnerLesson 8 of 305 min read

Dictionaries and sets: lookups and unique values

Store data as key-value pairs in dictionaries, loop and update them, build counters and nested data, and use sets for unique values.

In this lesson you will

  • Create and update dictionaries
  • Loop with items()
  • Count and nest data
  • Use sets for unique values

About 20 minutes

Looking things up by name

A list finds things by position: “the third item”. But a phone book finds things by name: look up “Sara” and get her number. Python’s dictionary works exactly like that. Each entry has a key (the name you look up) and a value (what you get back). Dictionaries are everywhere: a user profile, a product, a settings file, the data an API sends you.

Creating and reading dictionaries

user = {
    "name": "Sara",
    "age": 28,
    "city": "Lahore",
    "is_member": True,
}

print(user["name"])
print(user["age"] + 1)
print(user.get("email"))               # missing key: None, no error
print(user.get("email", "no email"))   # with a default
print(len(user))
Sara
29
None
no email
4
user = {"name": "Sara"}
print(user["email"])
Traceback (most recent call last):
  ...
KeyError: 'email'

Square brackets raise a KeyError if the key is missing. The .get() method is the polite alternative: it returns None or a default you choose.

Adding, changing and removing

user = {"name": "Sara", "age": 28}

user["age"] = 29                 # change
user["email"] = "sara@mail.com"  # add a new key
print(user)

removed = user.pop("email")      # remove and return
print(removed)
del user["age"]
print(user)

print("name" in user)            # check for a key
print("age" in user)
{'name': 'Sara', 'age': 29, 'email': 'sara@mail.com'}
sara@mail.com
{'name': 'Sara'}
True
False

Looping over a dictionary

prices = {"pen": 20, "bag": 900, "ruler": 60}

for item in prices:                    # loops over keys
    print(item)

for item, price in prices.items():     # keys and values together
    print(f"{item}: Rs {price}")

print(list(prices.keys()))
print(list(prices.values()))
print(sum(prices.values()))
pen
bag
ruler
pen: Rs 20
bag: Rs 900
ruler: Rs 60
['pen', 'bag', 'ruler']
[20, 900, 60]
980

items() is the one you will use most. It hands you each key and value as a pair, which you can unpack straight into two variables in the for line.

Dictionaries as counters

text = "the cat and the hat and the bat"
counts = {}

for word in text.split():
    counts[word] = counts.get(word, 0) + 1

print(counts)

best = max(counts, key=counts.get)
print("Most common:", best, counts[best])
{'the': 3, 'cat': 1, 'and': 2, 'hat': 1, 'bat': 1}
Most common: the 3

The line counts[word] = counts.get(word, 0) + 1 reads: take the current count (zero if we have not seen the word yet), add one, store it back. The standard library also offers collections.Counter to do this in one step.

from collections import Counter

print(Counter("mississippi"))
print(Counter("the cat and the hat".split()).most_common(1))
Counter({'i': 4, 's': 4, 'p': 2, 'm': 1})
[('the', 2)]

Nested dictionaries and lists of dictionaries

students = [
    {"name": "Sara", "marks": {"maths": 90, "english": 75}},
    {"name": "Ali",  "marks": {"maths": 62, "english": 88}},
]

print(students[0]["marks"]["maths"])

for s in students:
    average = sum(s["marks"].values()) / len(s["marks"])
    print(f"{s['name']}: {average:.1f}")

top = max(students, key=lambda s: s["marks"]["maths"])
print("Top in maths:", top["name"])
90
Sara: 82.5
Ali: 75.0
Top in maths: Sara

This shape, a list of dictionaries, is how nearly all real-world data arrives: rows from a database, items from a web API, records from a spreadsheet. It is the same as JSON, the format used across the web, and Python converts between them with the json module.

import json

data = {"name": "Sara", "skills": ["python", "css"], "active": True}
text = json.dumps(data)
print(text)

back = json.loads(text)
print(back["skills"][0])
{"name": "Sara", "skills": ["python", "css"], "active": true}
python

Dictionary comprehensions and merging

prices = {"pen": 20, "bag": 900, "ruler": 60}

sale = {item: price * 0.9 for item, price in prices.items()}
print(sale)

cheap = {k: v for k, v in prices.items() if v < 100}
print(cheap)

defaults = {"theme": "light", "size": 14}
custom = {"size": 18}
merged = defaults | custom          # later values win
print(merged)
{'pen': 18.0, 'bag': 810.0, 'ruler': 54.0}
{'pen': 20, 'ruler': 60}
{'theme': 'light', 'size': 18}

Sets: unique values

A set is a collection with no duplicates and no order. It is perfect for removing repeats and for asking “is this in the group?” very quickly.

tags = ["css", "js", "css", "html", "js"]
unique = set(tags)
print(unique)
print(len(unique))

a = {1, 2, 3, 4}
b = {3, 4, 5}
print(a | b)    # union: everything
print(a & b)    # intersection: in both
print(a - b)    # difference: in a but not b

seen = set()
seen.add("pen")
seen.add("pen")
print(seen)
print("pen" in seen)
{'js', 'html', 'css'}
3
{1, 2, 3, 4, 5}
{3, 4}
{1, 2}
{'pen'}
True
Good to know

An empty {} creates a dictionary, not a set. For an empty set, write set().

Choosing the right collection

Need Use
An ordered list that changes list
A fixed group of values tuple
Look up a value by name dict
Unique values / fast membership tests set
Practice

Store the prices {"tea": 80, "cake": 150, "juice": 120} in a dictionary. Print the most expensive item’s name, and the total if you buy one of each.

Show one possible answer
menu = {"tea": 80, "cake": 150, "juice": 120}
print(max(menu, key=menu.get))
print(sum(menu.values()))
cake
350
Practice

Count the vowels in the sentence "Dictionaries are very useful" and print the counts as a dictionary.

Hint: Use counts.get(ch, 0) + 1.

Show one possible answer
text = "Dictionaries are very useful"
counts = {}
for ch in text.lower():
    if ch in "aeiou":
        counts[ch] = counts.get(ch, 0) + 1
print(counts)
{'i': 3, 'o': 1, 'a': 2, 'e': 4, 'u': 2}
Common mistake

Using a list as a dictionary key. Keys must be unchangeable (strings, numbers, tuples). {[1, 2]: "x"} raises TypeError: unhashable type: 'list'. Use a tuple (1, 2) instead.

Prefer .get() for optional data

When data may be missing, .get(key, default) keeps your program calm. Use [key] only when a missing key would truly be a bug that you want to hear about.

Check what you learned

Five quick questions. Pick an answer and you will see straight away whether it is right and why.

How do you read the value for key "name" safely, with a default?

  1. d("name")
  2. d.name
  3. d.read("name")
  4. d.get("name", "unknown")

get returns the default instead of raising KeyError.

Which loop gives both keys and values?

  1. for k in d.values()
  2. for k, v in d.items()
  3. for v in d.keys()
  4. for d in items

items() yields key and value pairs.

What does a set do with duplicates?

  1. Keeps all
  2. Raises an error
  3. Sorts them
  4. Keeps only one of each

Sets store unique values.

What does {} create?

  1. An empty dictionary
  2. An empty set
  3. An empty list
  4. An error

Use set() for an empty set.

Which can be a dictionary key?

  1. A list
  2. A dictionary
  3. A string or a tuple
  4. A set

Keys must be immutable.

Summary

  • Dictionaries map keys to values.
  • Use get() for optional keys and items() to loop.
  • Lists of dictionaries are the shape of real data.
  • Sets keep unique values and support set maths.

Next lesson

Next: functions, reusable blocks of code with names.

Questions

Are dictionaries ordered?

Yes. Since Python 3.7 they remember the order items were added.

Dictionary or list of tuples?

Use a dictionary when you look things up by key. It is much faster and clearer.

How do I sort a dictionary?

Use sorted(d.items(), key=...) which gives a list of pairs, or build a new dict from it.

Progress is saved in this browser.