5.7. Dictionaries#

A dictionary (mostly unrelated to a data dictionary which we saw earlier) is a versatile data structure that allows you to store data as key-value pairs. It is similar to a real-world dictionary where you look up a word (the key) to find its definition (the value).

Dictionaries are:

  • Structured with key-value pairs. Each dictionary consists of a unique key and its associated values. Keys must be unique.

  • Unordered. There is no innate order for the key-value pairs in a dictionary. In recent versions of Python dictionaries will maintain the order which the key-value pairs were inserted, but this is not true in other languages.

  • Mutable. Unlike records, you can add, remove or modify entries after the dictionary is created.

  • Efficiently accessed. Dictionaries are typically optimised for the fast retrieval based on their keys. In Python this is achieved by implementing them as a hash map, which is a data structure you’ll learn about later.

5.7.1. Creating A Dictionary#

To create a dictionary in python you can use braces (i.e. {}). Elements in the dictionary are key value pairs where the key and pair are separated by a colon : and elements are separated by a comma ,. The format of a dictionary is as follows

dictionary = {key_1:value_1,
                ...
            key_n:value_n}

Here’s an example

wattle_high_school = {
    "Name": "Wattle High School",
    "Students": 84,
    "Motto": "Aliquam libero sit amet!",
}

We can create an empty dictionary just using braces by themselves, i.e. {} the same way we can make an empty list with [] and an empty tuple with ().

5.7.2. Accessing And Modifying A Dictionary#

Accessing values from a dictionary is easy! Simply provide a key as an index:

wattle_high_school = {'Name': 'Wattle High School',
                      'Students': 84,
                      'Motto': 'Aliquam libero sit amet!'}

print(wattle_high_school['Students'])
84

To add or modify a key-value pair we access the element we want and overwrite it.

dictionary[key] = value
wattle_high_school = {'Name': 'Wattle High School',
                      'Students': 84,
                      'Motto': 'Aliquam libero sit amet!'}

# Add a key-value pair
wattle_high_school['Colours'] = 'Green and yellow'
print(wattle_high_school['Colours'])

# Modify a key-value pair
wattle_high_school['Students'] = 88
print(wattle_high_school['Students'])
Green and yellow
88

To delete a key-value pair we use del and access the element we want to delete.

del dictionary[key]
wattle_high_school = {'Name': 'Wattle High School',
                      'Students': 84,
                      'Motto': 'Aliquam libero sit amet!'}

# Remove a key-value pair
del wattle_high_school['Motto']

print(wattle_high_school)
{'Name': 'Wattle High School', 'Students': 84}

Dictionaries are typically used in cases where you want to represent structured data (like a record) but you want mutability or in cases in which you want to track relationships between quantities (e.g. your contacts list).

There are also a few functions which allow you to iterate through the dictionary.

  • .keys() will give you a list of keys (in a random order)

  • .values() will give you a list of values (in a random order)

  • .items() will give you a list of key-value tuples (in a random order)

wattle_high_school = {'Name': 'Wattle High School',
                      'Students': 84,
                      'Motto': 'Aliquam libero sit amet!'}
print(wattle_high_school.keys())
print(wattle_high_school.values())
print(wattle_high_school.items())
dict_keys(['Name', 'Students', 'Motto'])
dict_values(['Wattle High School', 84, 'Aliquam libero sit amet!'])
dict_items([('Name', 'Wattle High School'), ('Students', 84), ('Motto', 'Aliquam libero sit amet!')])

You can use any of these to iterate through the dictionary.

With .keys()

wattle_high_school = {'Name': 'Wattle High School',
                      'Students': 84,
                      'Motto': 'Aliquam libero sit amet!'}
for key in wattle_high_school.keys(): # for key in wattle_high_school also works
    print(key)
Name
Students
Motto

With .values()

wattle_high_school = {'Name': 'Wattle High School',
                      'Students': 84,
                      'Motto': 'Aliquam libero sit amet!'}
for value in wattle_high_school.values():
    print(value)
Wattle High School
84
Aliquam libero sit amet!

With .items()

wattle_high_school = {'Name': 'Wattle High School',
                     'Students': 84,
                     'Motto': 'Aliquam libero sit amet!'}
for key, value in wattle_high_school.items():
    print('{}: {}'.format(key, value))
Name: Wattle High School
Students: 84
Motto: Aliquam libero sit amet!

5.7.3. Dictionary Errors#

If you try to access a dictionary element using a key that does not exist, you will get a KeyError.

wattle_high_school = {
    "Name": "Wattle High School",
    "Students": 84,
    "Motto": "Aliquam libero sit amet!",
}
print(wattle_high_school["Address"])
Traceback (most recent call last):
    File "/home/main.py", line 4, in <module>
        print(wattle_high_school['Address'])
            ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^
    KeyError: 'Address'
Question 1

What would be the output of the following program?

schedule = {'start': '9:00am',
    'morning tea': '10:15am',
    'lunch': '12:50pm',
    'end': '3:25pm'}
print(schedule['lunch'])
  1. {'start': '9:00am', 'morning tea': '10:15am', 'lunch': '12:50pm', 'end': '3:25pm'}
    
  2. {'lunch': '12:50pm'}
    
  3. 12:50pm
    
Solution

C.

Question 2

What is the state of the dictionary after the following code:

fruits = {}

fruits['Avocado'] = 'Tasty'
fruits['Strawberry'] = 'Extra tasty'
fruits['Kiwifruit'] = 'Green'

fruits['Strawberry'] = 'Sometimes mouldy, makes you sick'
  1. {'Avocado': 'Tasty', 'Strawberry': 'Sometimes mouldy, makes you sick', 'Kiwifruit': 'Green'}
    
  2. {'Avocado': 'Tasty', 'Strawberry': 'Extra tasty', 'Kiwifruit': 'Green'}
    
  3. {'Avocado': 'Tasty', 'Strawberry': 'Extra tasty', 'Kiwifruit': 'Tasty'}
    
  4. {'Avocado': 'Green', 'Strawberry': 'Red', 'Kiwifruit': 'Green'}
    
Solution

Solution is locked

Question 3

Consider the following code:

dictionary = {1: 'Not prime', 2: 'Prime', 3: 'Prime', 4: 'Not prime', 5: 'Prime'}
print(dictionary[3])

What will be the output?

  1. Prime

  2. Not prime

Solution

Solution is locked

Question 4

What is the state of the dictionary after the following code:

dictionary = {1: 'Not prime', 2: 'Prime', 3: 'Prime', 4: 'Not prime', 5: 'Prime'}
del dictionary[4]
  1. {2: 'Prime', 3: 'Prime', 4: 'Not prime', 5: 'Prime'}
    
  2. {1: 'Not prime', 3: 'Prime', 4: 'Not prime', 5: 'Prime'}
    
  3. {1: 'Not prime', 2: 'Prime', 4: 'Not prime', 5: 'Prime'}
    
  4. {1: 'Not prime', 2: 'Prime', 3: 'Prime', 5: 'Prime'}
    
  5. {1: 'Not prime', 2: 'Prime', 3: 'Prime', 4: 'Not prime'}
    
Solution

Solution is locked

Code challenge: Price Check

Write some code to check the price of products in the supermarket.

The prices of each item have been stored in a dictionary called products.

Your script must:

  1. Ask for the name of a product

  2. Return the price

Example 1

Enter an item name: Milk
The cost of Milk is $1.99.

Example 2

Enter an item name: Oranges
The cost of Oranges is $2.48.
Solution

Solution is locked

Code challenge: Grades

Your teacher has asked you to write some code to make her marking easier.

She wants a python script that collects student grades. The script must:

  1. Ask for a student ID

  2. If no student ID is provided, terminate, continuing to step 5.

  3. Ask for the associated grade

  4. Store the student name and grade as a key-value pair in a dictionary called data

  5. Print out the number of grades entered

  6. Print out the final dictionary

Student IDs are strings and grades are integers.

Example 1

Student ID: 1234
Grade: 75
Student ID: 8976
Grade: 82
Student ID:
2 grades entered
{'1234': '75', '8976': '82'}

Example 2

Student ID: 3584
Grade: 65
Student ID: 4585
Grade: 84
Student ID: 1365
Grade: 74
Student ID: 3696
Grade: 91
Student ID:
4 grades entered
{'3584': '65', '4585': '84', '1365': '74', '3696': '91'}

Hint

You can use len to check the number of items in a dictionary.

fruits = {'Avocado': 'Tasty',
          'Strawberry': 'Extra tasty'}
print(len(fruits))
2
Solution

Solution is locked

Code challenge: Update Grades

You have been provided with a dictionary of students and grades.

A remarking of the exams was done, so some of the marks require updating.

Write some Python code that:

  1. Asks for a student ID.

  2. If no student ID is provided then terminate and proceed to step 6. Otherwise:

  3. Checks to see if the student ID exists in the dictionary. If the student does not exist in the dictionary, prints Student does not exist. Terminating and you should stop reading user input and proceed to step 6.

  4. Asks for the associated grade.

  5. Finds the corresponding key-value pair, and updates the grade of the student.

  6. Print out the updated data dictionary.

Student IDs are strings and grades are integers.

Example 1

Student ID: 1234
Grade: 89
Student ID: 8976
Grade: 80
Student ID:
{'1234': 89, '8976': 80, '4366': 62, '9832': 81, '8758': 90}

Example 2

Student ID: 4366
Grade: 65
Student ID: 5847
Student does not exist. Terminating
{'1234': 75, '8976': 89, '4366': 65, '9832': 81, '8758': 90}

Hint

To check if an item is in a list of values, you can use the in and not in keywords. For example:

nums = [6, 0, 3, -2, 2]

print(6 in nums)
print(10 not in nums)
True
True
Solution

Solution is locked

Code challenge: Dicrionaries Combined

You have been provided with two existing dictionaries.

Write a Python program to combine these two dictionaries so that the value in each key-value pair is a list containing the values from both dictionaries. The value from dict1 should be first in the list.

Print out the combined dictionary.

Note

You can assume each dictionary has the same keys.

dict1 = {'Roger' : 8,
         'Rafael' : 3,
         'Andy' : 3,
         'Novak' : 8}

dict2 = {'Roger' : -2,
         'Rafael' : 13,
         'Andy' : 0,
         'Novak' : 3}
Solution

Solution is locked