Dictionaries
Objectives
- Differentiate between dictionaries and lists in Python, emphasizing the key-value pair structure of dictionaries.
- Define a dictionary using curly braces {} or the dict() constructor.
- Understand the uniqueness and immutability of keys in a dictionary.
- Create an empty dictionary using curly braces and the dict() constructor.
- Initialize a dictionary with key-value pairs using identifiers and colons.
- Access values in a dictionary by specifying the key in square brackets.
- Use built-in methods such as keys(), values(), and items() to retrieve information from dictionaries.
A dictionary is similar to a list but rather than using integers to index the elements in the list the indices can be of any type. In other programming languages the dictionary might be known as a map or an associative array. Think od a conventional English to French dictionary, if you want to know the French word for say 'house', your would look up in the dictionary for the item (key) 'house', the read back the French equivalent (value) as 'la maison'. The notion of the key being associated with, or mapped to, a value is the essence of how the dictionary operates.
Example 11
Predict the output of the program for the highlighted lines in the example program:
- What will be printed for the original climate data?
- What will the temperature variable contain?
- How will the climate data look after updating the temperature?
- What will be printed for keys, values, and items?
Run the program in a Python environment to confirm or correct your predictions
Dictionary Basics
- A dictionary is defined using curly braces
{}
or thedict()
constructor. - Each item in a dictionary consists of a key and its corresponding value, separated by a colon
:
. - Keys are unique within a dictionary, and they must be immutable objects (e.g., strings, numbers, or tuples).
- Values can be of any data type, including numbers, strings, lists, other dictionaries, and more.
- Some similarity to a list, but the index used will be the key rather than a numerical position
Creating a Dictionary
We can also use the function dict()
, which creates an empty dictionary:
Note the output from the print()
statement: {}
. The curly braces signify the data type as being a dictionary.
Accessing Values
- You can access the values in a dictionary by specifying the key in square brackets
[]
. - If the key is not found, it raises a
KeyError
.
Recall the similarity with a list, we could have used a list to hold the climate data but this data would be 'keyed' by their position in the list e.g.climate_data[0]
. For the dictionary we use the identifier for the key, here being "temperature" in the square brackets. Using the identifier makes the code easier to read.
Modifying and Adding Entries
- You can modify the value associated with a key or add new key-value pairs to an existing dictionary.
Note
When adding elements to a dictionary the order is not known, the dictionary is not automatically sorted. However, this is not that significant as we use the key to access the associated element and the key must be unique. If the key does not exist in the dictionary we get a KeyValueError
returned as an exception.
Dictionary Methods and Functions
- Python provides several built-in methods and functions to work with dictionaries, such as
keys()
,values()
,items()
,get()
,pop()
, and more. - These methods allow you to retrieve keys, values, key-value pairs, and manipulate dictionary contents.
The return type from the keys()
and values()
functions are of type dict_keys()
and dict_values
respectively. To make them a little easier to handle it is recommended to convert them to a list first:
In addition functions such as len()
can be used to return the number of items in the dictionary.
Iterating Through a Dictionary
- You can iterate through a dictionary using a
for
loop, which by default iterates over keys. - The
in
operator can be applied which works on the keys in the dictionary
Note
The algorithm used by the in
operator is different for lists and dictionaries. For a list it uses a linear search and thus the search time is dependent on the length of the list. For the dictionary it uses a hash table which means it takes the same amount of time for any item irrespective of the number of items in the dictionary.
You might also try:
The output is:
These are tuples and will be looked at in a later section.
Checking for Key Existence
You can use the in
keyword or the get()
method to check if a key exists in a dictionary.
For line 6, there is no key for "rainfall" in the original dictionary therefore the variable rainfall
will be assigned "Not found". If it was present in the dictionary the variable would contain the key's value.
Note
- Dictionaries are unordered collections, which means the order of key-value pairs may not be preserved in older versions of Python (prior to 3.7).
- Starting from Python 3.7+, dictionaries maintain the insertion order of items.
Activity
Using the example code, make the following changes:
- Add a new key-value pair for precipitation
- Remove a key-value pair
- Update an existing value
- Iterate through the dictionary and print each key-value pair
Copy the following into your Python environment and modify the code as indicated by the comments.
Further Example
Climate Quest Project
Throughout this topic we'll be working on a large scale project: Climate Quest. In this project a player embarks on a journey to combat the effects of climate change by making decisions that impact the environment. Each choice affects the outcome of the game, emphasizing the importance of individual actions in addressing climate change.
Summary
Cheat sheet for dictionaries 1
Questions
Programming Tasks
For each of the following tasks write a program using Python.
Write a Python function that takes a dictionary of student names and their corresponding grades and returns the names of all students who received an 'A' grade.
Create a Python program that simulates a simple English-to-French dictionary. Allow the user to input an English word, and your program should return the French translation if it exists in the dictionary.
Write a Python function that takes a list of dictionaries, where each dictionary represents a book with attributes like title, author, pages, and year. The function should return a list of book titles published in a specific year provided as an argument to the function.