Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

2.3 (2 of 3): Dictionaries: look it up by name

The whole lesson, free. Read it and have a go at the activities. To work the tasks and labs in the real lesson, press Start lab, with no account needed.

45 minutes

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

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

WordWhat it meansExample
DictionaryA data structure that stores pairs. You look up a value by its key.{"Sam": 16}
KeyThe name you look up. Each key is different."Sam"
ValueThe data stored with a key.16
PairOne key with its value."Sam": 16
ListOne name that holds many values in order.[4, 8, 15]
IndexThe position number in a list. It starts at 0.0
Data structureA 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

  1. 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.
  2. 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.
  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.
  4. 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.
  5. 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.
  6. 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

Code: python
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

KeyValue
"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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

What you can do with a dictionary

CodeWhat it doesResult
menu["pie"] = 4Add a new pair, or change an old onepie is now 4
menu["tea"]Read a value by its keyfor example 2
"tea" in menuIs the key there?True or False
"pie" not in menuIs the key missing?True or False
menu.pop("tea")Remove the pair and give back its valuefor 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

Code: python
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

Code: python
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?

QuestionUse a listUse 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 orderYou look up by key, not by position
ExampleThe 10 scores in a quizA price for each item on a menu
Can two items be the same?YesKeys 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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Your turn: a cafe menu

A cafe keeps its prices in a dictionary. Item names are keys, prices are values.

  1. price_of gives back the price of one item.
  2. add_price adds an item and its price, and gives back the menu.
  3. can_order gives back True if the item is on the menu, and False if not.
  4. 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.

Code: python
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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Remember

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.

Teach this lesson with your class

On a college account your students work the tasks and the labs as well, and every mark is kept for you.