Dictionaries#
This notebook is based on materials kindly provided by the IN1900 team.
Dictionaries are lookup tables. We can use dictionaries to store and look up information. Rather than numerical indexes, a dictionary assigns a key to each stored value. Keys are usually strings, and we can use the key to find the value.
Like lists, dictionaries can store many different types of data.
In Python code dictionaries are enclosed in curly brackets: { }
.
dictionary = {}
print(type(dictionary))
<class 'dict'>
Creating Dictionaries#
We can create a new dictionary, and add some elements:
phone_directory = {"Alice": 12345679, "Bob": 2345678}
# Add an entry
phone_directory["Charlie"] = 456789
print(phone_directory)
{'Alice': 12345679, 'Bob': 2345678, 'Charlie': 456789}
Getting Items#
We can get items like we do with lists. However, with dictionaries we use keys rather than numerical indexes:
number = phone_directory["Alice"]
print(number)
12345679
Exercise: A Dictionary Describing a Book #
Create a dictionary containing these data about a book:
Title: Scenario
Author: Jon Bing
Then, print each value in the dictionary on the screen.
book = #Your solution
Cell In[4], line 1
book = #Your solution
^
SyntaxError: invalid syntax
Iterating Over Dictionaries#
When you iterate over dictionary, you iterate over the keys not the values.
for name in phone_directory:
number = phone_directory[name]
print(name, "has phone number", number)
Is this Key in the Dictionary?#
We can check for the presence of a key with the Boolean expression
key in dictionary
.
Boolean expressions are True
or False
.
print("Alice" in phone_directory)
print("Alex" in phone_directory)
Nested Dictionaries#
Like with nested lists, we can put dictionaries inside other dictionaries.
phone_directory = {"Alice": {"phone": 12345679,
"mobile": 45678},
"Bob": {"phone": 145679,
"mobile": 4578}}
for name in phone_directory:
numbers = phone_directory[name]
for device in numbers:
print(name, "has", device, "with number", numbers[device])
Exercise: A Nested Dictionary #
Below is a dictionary containing two books. Print the title of the first book, and the year of the second.
library = {'8251790530': {'title': 'Azur',
'author': 'Jon Bing',
'year': 1975},
'8247800497': {'title': 'Zalt',
'author': 'Jon Bing',
'year': 1976}}
#Your solution
Exercise: Iterating Over a Nested Dictionary #
We will continue using the dictionary library
from the previous exercise.
Now, use for
-loops to print out all the bibliographic data in the library
dictionary by iterating over the keys of the dictionaries.
You will need to use two nested for
-loops.
library = {'8251790530': {'title': 'Azur',
'author': 'Jon Bing',
'year': 1975},
'8247800497': {'title': 'Zalt',
'author': 'Jon Bing',
'year': 1976}}
for #Your solution
Exercise: A Very Nested Dictionary #
Index the dictionary people
in order to print out the phone number of Alice.
people = {'Alice': {'Age': 37, 'numbers': {'phone': 12345679,
'mobile': 45678}},
'Bob': {'Age': 42, 'numbers': {'phone': 145679,
'mobile': 4578}}}
#Your solution
Key Points#
A dictionary links values to keys:
phone_directory["Alice"]
Keys are usually strings, values can be any object
We can iterate over dictionaries