Dictionaries – Basics (creation, accessing), online

Six activities your students do on their own devices. See who's finished, and their scores.

Dictionaries – Basics (creation, accessing) in one minute

What students need to know

A dictionary stores data as key-value pairs inside curly braces: person = {'name': 'Alice'}. The key is the label used to look something up, and the value is the data stored under it. A value is accessed with square brackets around its key — person['name'] — which looks items up by name rather than by position, unlike a list.

Mistakes students make

  • Using square brackets to create a dictionary — person = ['name': 'Alice'] — is a syntax error; dictionaries need curly braces {}.
  • Looking up a key that doesn't exist raises a KeyError rather than returning nothing.

Discover other journeys with this module

What students will learn in this module

Every question in Module 37

Quick questions

  1. What is a dictionary?AAn ordered list of itemsBA collection of key-value pairs
  2. How do you create a dictionary?ACurly braces {}BSquare brackets []
  3. d = {'a': 1}\nprint(d['a'])\nWhat prints?A1B'a'
  4. What separates a key from its value?AA colon :BAn equals sign =
  5. Can dictionary keys be duplicated?AYesBNo each key must be unique

Key terms

Students type the meaning of:

  • Dictionary
  • Key
  • Value
  • dict[key]

Match the pairs

person = {'name': 'Ali'}
print(person['age'])
Match it to what it does
d = {name: 'Alice'}Match it to what it does
person = {'name': 'Ali', 'age': 20}Match it to what it does
d = {}
d['x'] = 10
Match it to what it does
p = {'name':'Ali'}
print(p['name'])
Match it to what it does
p = {}
print(type(p))
Match it to what it does

Fill in the blanks

  1. person = _'name': 'Alice'_
  2. print(person[______])
  3. d = {}
    d[_______] = 'blue'
  4. pet = {'type'_ 'cat'}
  5. scores = {'math': 90_ 'english': 85}

Spot the error

Question 1

A# Create a person dictionaryBperson = ['name': 'Alice']Cprint('Name:', person['name'])Dprint('Done')

Question 2

A# Create a person dictionaryBperson = {'name' = 'Alice'}Cprint('Name:', person['name'])Dprint('Done')

Question 3

Aperson = {'name': 'Alice'}Bgreeting = person[name]Cprint('Hello,', greeting)Dprint('Done')

Question 4

Aperson = {'name': 'Alice'}Bgreeting = person('name')Cprint('Hello,', greeting)Dprint('Done')

Question 5

Aperson = {'name': 'Alice'}Blabel = 'Greeting'Cgreeting = person.nameDprint('Hello,', greeting)