Your phone contacts
Your phone has contacts. You do not look for the third contact. You type a name, and the phone gives you the number.
A dictionary is a data structure that works the same way. You give it a name. It gives you the matching value.
Key words
| Word | What it means | Example |
|---|---|---|
| Dictionary | A data structure that stores pairs. You look up a value by its key. | {"Sam": 16} |
| Key | The name you look up. Each key is different. | "Sam" |
| Value | The data stored with a key. | 16 |
| Pair | One key with its value. | "Sam": 16 |
| List | One name that holds many values in order. | [4, 8, 15] |
| Index | The position number in a list. It starts at 0. | 0 |
| Data structure | A way to organise many values. | list, dictionary |
The cells students fill in are left blank in this preview.
Watch: look it up by name
Press Play on the short film below. Watch each key and the value that goes with it: you find a value by its key, not by a number.
Film: dictionaries, look it up by name
Dictionaries: look it up by name
Every scene, as text
- A dictionary stores pairs, inside curly brackets. In ages = {"Sam": 16, "Aisha": 17, "Leo": 15} each pair is a key, the name you look up, then a colon, then its value, the data stored with that key.
- Put a key in square brackets to look up its value. print(ages["Aisha"]) shows 17: found by its key, not by a number. len, short for length, counts the pairs, so print(len(ages)) shows 3.
- Myth (false): a dictionary has an index. With a list, [0] is the first item; with a dictionary, [0] asks for a key called 0. The keys of ages are "Sam", "Aisha" and "Leo", so print(ages[0]) stops with KeyError: 0.
- Looking up a key that is not there stops the program: with prices = {"tea": 2}, print(prices["pie"]) stops with KeyError: 'pie'. So use in first: print("pie" in prices) shows False and print("tea" in prices) shows True.
- Add and change with =. With menu = {"tea": 2, "cake": 3}, menu["pie"] = 4 adds a new pair, and menu["tea"] = 2.5 changes the value of "tea", because "tea" is already a key and a key is never repeated. menu now holds "tea": 2.5, "cake": 3 and "pie": 4.
- List or dictionary? A list finds a value by its position (index): print(scores[0]) shows 4, the first score, like 10 quiz scores in order. A dictionary finds a value by its key (a name): print(menu["tea"]) shows 2, like a price for each item on a menu.
Try it: make a dictionary
ages = {"Sam": 16, "Aisha": 17, "Leo": 15}
print(ages)
print(ages["Aisha"])
print(len(ages))
# Look up Leo instead of Aisha.
Make a free account to save your work and track your progress.
The dictionary as a table
| Key | Value |
|---|---|
| "Sam" | 16 |
| "Aisha" | 17 |
| "Leo" | 15 |
The cells students fill in are left blank in this preview.
A dictionary stores pairs
A dictionary stores pairs. Each pair is a key and a value, with a colon between them. The whole dictionary is inside curly brackets.
You find a value by its key, not by a number. Every key must be different.
Read the dictionary
Have a go
Read the dictionary
3 questionsWhat you can do with a dictionary
| Code | What it does | Result |
|---|---|---|
| menu["pie"] = 4 | Add a new pair, or change an old one | pie is now 4 |
| menu["tea"] | Read a value by its key | for example 2 |
| "tea" in menu | Is the key there? | True or False |
| "pie" not in menu | Is the key missing? | True or False |
| menu.pop("tea") | Remove the pair and give back its value | for example 2 |
| len(menu) | How many pairs? | for example 3 |
The cells students fill in are left blank in this preview.
Try it: change a dictionary
menu = {"tea": 2, "cake": 3}
menu["pie"] = 4
menu["tea"] = 2.5
print(menu)
print("cake" in menu)
print(menu.pop("cake"))
print(menu)
print(len(menu))
# Predict each line before you run it.
Make a free account to save your work and track your progress.
Try it: visit every pair
ages = {"Sam": 16, "Aisha": 17}
for name in ages:
print(name, "is", ages[name])
# The loop gives each key. ages[name] gives its value.
Make a free account to save your work and track your progress.
List or dictionary?
| Question | Use a list | Use a dictionary |
|---|---|---|
| How do you find a value? | By its position (index) | By its key (a name) |
| Does the order matter? | Yes, it keeps the order | You look up by key, not by position |
| Example | The 10 scores in a quiz | A price for each item on a menu |
| Can two items be the same? | Yes | Keys must be different. Values can match. |
The cells students fill in are left blank in this preview.
Match the word to its meaning
Have a go
Loading…
Your answers are marked here and are not kept. A college account keeps every student’s marks.
Which data structure?
Have a go
Which data structure?
4 questionsYour turn: a cafe menu
A cafe keeps its prices in a dictionary. Item names are keys, prices are values.
- price_of gives back the price of one item.
- add_price adds an item and its price, and gives back the menu.
- can_order gives back True if the item is on the menu, and False if not.
- bill adds up the price of every item in a list called order. An item can be in the order twice.
Press Run Tests to check.
def price_of(menu, item):
# Give back the price of the item.
return None
def add_price(menu, item, price):
# Add the item and its price to the menu. Give back the menu.
return None
def can_order(menu, item):
# True if the item is on the menu.
return None
def bill(menu, order):
# order is a list of item names. Add up the price of each one.
return None
Make a free account to save your work, or press Start lab to work through the whole lesson.
Key words crossword
Have a go
Loading…
Your answers are marked here and are not kept. A college account keeps every student’s marks.
Exit quiz
Have a go
Exit quiz
6 questionsRemember
Important
A dictionary stores key and value pairs. Look up by key. Add and change with =. Use in to check a key, and pop to remove one.