Dictionaries#

Author: Mike Wood

Learning objectives: By the end of this notebook, you should be able to:

  1. Create and edit Python dictionaries

  2. Describe the types of items that can used as keys in a dictionary

  3. Call simple dictionary methods to modify dictionary contents

Creating and Editing Dictionaries#

Dictionaries are used as a “look-up table” that associate two sets of objects. The items or “keys” in a dictionary must be unique but the correspondence does not need to be one-to-one.

# make a dictionary to reference integers to their string representations
string_numbers = {1:"one", 2:"two", 3:"three"}

To access the value corresponding to a key in the dictionary, pass the key:

# get the string representation of the integer 2
string_numbers[2]
'two'

We can access all of the look-up values with the .keys() method:

# use the keys() method to access the dictionary keys
string_numbers.keys()
dict_keys([1, 2, 3])

It’s important to note that the keys aren’t a list:

# check the type of the dict keys
print(type(string_numbers.keys()))

# try to access one of the keys using an index - what happens?
# uncomment the following line to test this out
# string_numbers.keys()[1]
<class 'dict_keys'>

Dictionaries are flexible and can be resized as needed. Editing a dictionary is similar to lists: dictionaries have a pop method for removing keys and keys can be (re-)assigned in a dictionary using the index notation:

# remove the 2:"two" key value pair using the pop method:
string_numbers.pop(2)

# add a 4:"four" key-value pair using index notation:
string_numbers[4] = "four"

# print my_dict
print(string_numbers)
{1: 'one', 3: 'three', 4: 'four'}

🤔 Mini-Exercise#

Make a dictionary for the following names of classes:

Department Code

Course Number

Course Name

MATH

123

Advanced Calculus

MATH

133

Partial Differential Equations

CS

123

Bioinformatics

CS

133

Introduction to Data Visualization

You should be able to pass a pair of values corresponding to the department code (str) and the course number (int) and the dictionary should return the course name.

đź’ˇ Solution#

One possible solution to this scenario is to use a tuple that contains a string for the department and an integer for the course number. Note that you cannot use a list for a dictionary key because lists are mutable and not hashable.

# create your dictionary here
course_dictionary = {('MATH',123): 'Advanced Calculus',
                    ('MATH',133): 'Partial Differential Equations',
                    ('CS',123): 'Bioinformatics',
                    ('CS',133): 'Introduction to Data Visualization'}

# test that your dictionary works
course_dictionary[('MATH',123)]
'Advanced Calculus'

Common Dictionary Methods#

The following table lists some common dictionary methods:

Method

Description

keys()

Views all the keys in the dictionary

values()

Views all the values in the dictionary

get()

Returns the value of a key if it exists, otherwise returns a default

copy()

Makes a copy of the dictionary

update()

Updates the entries of a dictionary given those in a second dictionary

Using get rather than a key explicitly#

The get method provides us a little bit of flexibility in retrieving values from a dictionary. Let’s take a look at an example:

# define a dictionary with two entries
name_dict = {'first name':'Mike', 'last name':'Wood'}

# the get method works the same as acessing with a key as long
# as the entry is in the dict
print(name_dict['first name'])
print(name_dict.get('first name', ''))

# but it is beneficial to use in case the entry cannot be found
print(name_dict.get('middle name', ''))
# uncomment the following line to see what happens when
# a key is not in the dictionary
# print(name_dict['middle name'])
Mike
Mike

Combining Dictionaries#

Dictionaries can be combined using a union (with the pipe symbol |) or in place.

# As of Python 3.9, the union operator can be used to join two dictionaries
dict_A = {'a': 'A'}
dict_B = {'b': 'B'}
dict_C = dict_A | dict_B
print(dict_C)

# However, this does not change the dictionary contents
print(dict_A)

# If you want to join in place, use the update method
dict_A.update(dict_B)
print(dict_A)
{'a': 'A', 'b': 'B'}
{'a': 'A'}
{'a': 'A', 'b': 'B'}

âť— Syntax Warning#

Python uses the same syntax for sets and dictionaries – the curly braces {}. The only difference is that dictionary entries are included in pairs separated by colons while sets have entries like a list.

For example, suppose you intended to make a dictionary of numbers but accidentally forgot the colons i.e.

# example of an error in creating a dictionary - it's actually a set!
my_dict = {1, 'one', 2, 'two'}

Then, later in your code, you try to access a key-value pair, i.e.

# get the value corresponding to 1
my_dict.get(1)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[10], line 2
      1 # get the value corresponding to 1
----> 2 my_dict.get(1)

AttributeError: 'set' object has no attribute 'get'

In this example, we might be confused about the error because we intended to create a dictionary and clearly 1 is included in my_dict. However, we actually made a set! Hopefully this small example helps in interpreting and/or avoiding this code in your work.