
Dictionary Keys and Values
A Python dictionary is an unordered collection. Dictionaries are written with curly brackets, and they have keys and values.
For instance, have a look at the following example, where you store the details of an employee:
employee = {
'name': "Jack Nelson",
'age': 32,
'department': "sales"
}
You might have noticed a certain resemblance between Python dictionaries and JSON. Although you can load JSON directly into Python, a Python dictionary is a complete data structure that implements its own algorithms, and JSON is just a pure string written in a similar format..
Python dictionaries are something similar to key-value pairs. They simply map keys to associated values, as shown in Figure 2.12:

Figure 2.12: Mapping keys and values in Python dictionaries
Dictionaries are like lists. They both share the following properties:
- Both can be used to store values.
- Both can be changed in place and can grow and shrink on demand.
- Both can be nested: a dictionary can contain another dictionary, a list can contain another list, and a list can contain a dictionary and vice versa.
The main difference between lists and dictionaries is how elements are accessed. List elements are accessed by their position index, which is [0,1,2…] while dictionary elements are accessed via keys. Therefore, a dictionary is a better choice for representing collections, and mnemonic keys are more suitable when a collection's items are labeled, for instance, a database record such as Figure 2.13. The database here is equivalent to a list, and the database list contains a record which can be represented using a dictionary. Within each record, there are fields to store respective values, and a dictionary can be used to store a record with unique keys that are mapped to values:

Figure 2.13: A sample database record
There are, however, a few rules that you need to remember with Python dictionaries:
- Keys must be unique — no duplicate keys are allowed.
- Keys must be immutable — they can be a string, a number, or a tuple.
You will work with dictionaries and store a record in Exercise 28, Using a Dictionary to Store a Movie Record.
Exercise 28: Using a Dictionary to Store a Movie Record
In this exercise, you will be working with a dictionary to store movie records, and you will also try and access the information in the dictionary using a key. The following steps will enable you to complete the exercise:
- Open a Jupyter Notebook.
- Enter the following code in a blank cell:
movie = {
"title": "The Godfather",
"director": "Francis Ford Coppola",
"year": 1972,
"rating": 9.2
}
Here, you have created a movie dictionary with a few details, such as title, director, year, and rating.
- Access the information from the dictionary by using a key. For instance, you can use 'year' to find out when the movie was first released:
print(movie['year'])
You should get the following output:
1972
- Now update a dictionary value:
movie['rating'] = (movie['rating'] + 9.3)/2
print(movie['rating'])
You should get the following output:
9.25
As you can see, a dictionary's values can also be updated in place.
- Construct a movie dictionary from scratch and extend it using key-value assignment:
movie = {}
movie['title'] = "The Godfather"
movie['director'] = "Francis Ford Coppola"
movie['year'] = 1972
movie['rating'] = 9.2
As you may have noticed, similar to a list, a dictionary is flexible in terms of size.
- You can also store a list inside a dictionary and store a dictionary within that dictionary:
movie['actors'] = ['Marlon Brando', 'Al Pacino', 'James Caan']
movie['other_details'] = {
'runtime': 175,
'language': 'English'
}
print(movie)
You should get the following output:

Figure 2.14: Output while storing a dictionary within a dictionary
Up to this point, you have seen how easy it is to implement nesting in both lists and dictionaries. By combining lists and dictionaries creatively, we can store complex real-world information and model structures directly and easily. This is one of the main benefits of scripting languages such as Python.
Activity 7: Storing Company Employee Table Data Using a List and a Dictionary
Remember the employee dataset, which you previously stored using a nested list? Now that you have learned about lists and dictionaries, you will see how you can store and access our data more effectively using these two data types — lists and dictionaries:

Figure 2.15: Employee data in a table
Follow these steps to complete this activity:
- Open a Jupyter notebook (you can create a new one or use an existing one).
- Create a list named employees.
- Create three dictionary objects inside employees to store the information of each employee.
- Print the employees variable.
- Print the details of all employees in a presentable format.
- Print only the details of Sujan Patel.
You should get the following output:

Figure 2.16: Output when we only print the employee details of Sujan Patel
Note
The solution for this activity is available on page 526.
Zipping and Unzipping Dictionaries Using zip()
Sometimes, you obtain information from multiple lists. For instance, you might have a list to store the names of products and another list just to store the quantity of those products. What you can do is to aggregate lists using the zip() method.
The zip() method maps a similar index of multiple containers so that they can be used just as a single object. You will try it out in the following exercise.
Exercise 29: Using the zip() Method to Manipulate Dictionaries
In this exercise, you will be working on the concept of dictionaries, but you will be focusing on manipulating them by combining different types of data structures. You will be using the zip() method to manipulate the dictionary with our shopping list. The following steps will help you to understand the zip() method:
- Open up a new Jupyter Notebook.
- Now create a new cell and type in the following code:
items = ['apple', 'orange', 'banana']
quantity = [5,3,2]
Here, you have created a list of items and a list of quantity. Also, you have assigned values to these lists.
- Now, use the zip() function to combine the two lists into a list of tuples:
orders = zip(items,quantity)
print(orders)
This gives us a zip() object with the following output:
<zip object at 0x0000000005BF1088>
- Enter the following code to turn that zip object into a list:
orders = zip(items,quantity)
print(list(orders))
You should get the following output:
[('apple', 5), ('orange', 3), ('banana', 2)]
- You can also turn a zip object into a tuple:
orders = zip(items,quantity)
print(tuple(orders))
You should get the following output:
(('apple', 5), ('orange', 3), ('banana', 2))
- You can also turn a zip() object into a dictionary:
orders = zip(items,quantity)
print(dict(orders))
You should get the following output:
{'apple': 5, 'orange': 3, 'banana': 2}
Did you realize that you have to call orders = zip(items,quantity) every time? In this exercise, you will have noticed that a zip() object is an iterator and, therefore, once it has been converted to a list, tuple, or dictionary, it is considered a full iteration and it will not be able to generate anymore values.