Tutorpip← About this lessonOpen the sheet →Download PDF
Ages 10+ · Coding · Variables and data
Dictionaries and tables
Key (look this up)• apple• cookie• juice• soup• giant cookieValue (you get this)• 1 coin• 2 coins• 3 coins• 4 coins• 5 coins
A key leads straight to its value.
The big ideaA dictionary holds pairs, and you look up a key to get its value.

How it works

A dictionary is a collection of key and value pairs. You look up a key to get its value.

No counting boxes and no searching: the key leads straight to its value.

Each key may appear only once, or the lookup is unclear.

Add more columns and a dictionary becomes a table, with one row for each key.

A list fits• steps to wash hands• racers in finishing order• songs played in orderA dictionary fits• words and meanings• snacks and prices• names and colors
A list, or a dictionary?

Example 1: Look up a key

  1. Ravi writes each snack beside its price: apple 1 coin, cookie 2, juice 3 and soup 4.
  2. Each snack is a key, and its price is the value in its pair.
  3. Give it the key juice, and it hands back the value 3 coins.

Example 2: Each key once

  1. Ravi adds a giant cookie and names it cookie too.
  2. Now two rows are called cookie, so which price does the key mean?
  3. The mix-up to fix: rename the new one giant cookie, which costs 5 coins.

Example 3: A table

  1. Snack, price and how many are left are three columns.
  2. Each row is one record, and there is one row for each snack.

Example 4: A word book

  1. A word book works like a dictionary.
  2. A word is the key, and its meaning is the value.
  3. You look up the word and read its meaning.

Words to know

dictionary
A collection of key and value pairs.
key
The thing you look up, like a snack.
value
What you get back, like its price.
table
A dictionary with more columns, one row for each key.
record
One row of a table.

Watch out

Try it

  1. In the cafe table, is juice the key or the value?
  2. What value does the key soup give back?
  3. Can two keys be named cookie?
  4. Do racers in finishing order fit a list or a dictionary?
  5. Cookie costs 2 coins and giant cookie costs 5. How many more coins is the giant cookie?
Answers: 1. The key · 2. 4 coins · 3. No · 4. A list · 5. 3
Try at homeWrite the names of five family members or pets down one side of a paper and a favourite food or colour beside each. Ask a grown-up to say a name. You look it up and read the answer. Is any name used twice? What would you do?
For grown-upsDo the lookup Mission: write five names down one side of a paper and a favorite food beside each, then say a name for your child to look up. Ask what they would do if a name were used twice.
Open these notes on screenScan the code, or go to tutorpip.com.