In this lesson, we will learn about a new type of data structure called dictionaries, with a focus on the following key points,
- What are dictionaries and how are they used?
- Working with dictionaries in Python.
For the Javascript version of this lesson, please click here.
Previously, we've studied lists and other data types that can only store a single value of an element. Sometimes, you may encounter situations during your program when you need to refer to your data in pairs. In that case, dictionaries
are used. They are a data structure in Python that used a technique called hashing
to store the data in key-value
pairs.
Suppose you want to store information of students in a class, along with information identifiers. In this case, using variables or lists is not recommended as you will have to create too many variables and separating identifiers and their values from the list will be difficult. You will need something that stores and categorizes your data according to the identifiers. Dictionaries are excellent for this purpose as they allow programmers to categorize data into key-value
pairs, that is, you can specify multiple values under a single identifier. This makes the problem of storing student information easier!
Dictionaries
Dictionaries allow for the storage of data in the form of key-value
pairs. Keys
are unique dictionary elements that are assigned a value
. A key-value pair
is termed as an item
. You can consider keys as indices
of a dictionary. That is, the values stored in a dictionary can be accessed using a key
. Moreover, unlike lists, dictionaries can store a collection of elements (using a list or tuple) as a value for a single key.
Let's look at few examples of dictionaries in Python.
1student = {"ID": "01", "name":"Patrick", "age":18, "major":"Biology"}
Visualizing this example, we see that the data is stored somewhat like the illustration below.

If we want to store multiple values for a single key, all the values are grouped in a list (or a tuple) and assigned to the corresponding key.
1students = {"ID": ["01", "02", "03", "04"], "name":["Patrick", "William", "Elizabeth", "John"], "age":[18, 19, 19, 20], "major":["Biology", "CS", "Literature", "Mathematics"]}
Storing multiple values within a single key stores the data in the form of tables within the program.

To access values in a dictionary, keys are used as indices with the dictionary.
1student = {"ID": "01", "name":"Patrick", "age":18, "major":"Biology"}
2
3print(student["name"])
Properties of Dictionaries
- Dictionaries cannot have duplicate keys. That is, two keys of the same name in the same dictionary will generate an error.
- Values assigned to keys in a dictionary can be of any data type (as shown in the examples in the previous section).
- In Python, dictionaries are ordered. This means that they will be sorted in a defined order, and that order cannot change. For example, you cannot move the key-value pair from the first position in the dictionary to the last position.
- Keys of a dictionary must be immutable. This means that you can use strings, integers, or tuples as dictionary keys, but lists are not allowed.
In programming,
immutable
means that the value of a variable cannot be changed over time. Strings, int, and tuples are examples of immutable data types in Python.
Build your intuition. Is this statement true or false?
A dictionary can have two same keys or two same values.
Press true if you believe the statement is correct, or false otherwise.
Are you sure you're getting this? Click the correct answer from the options.
What will be the outcome of the following code block on execution?
1example_dict = {"a": 1, "b":2, "c":3, "d":4}
2print(example_dict[0])
Click the option that best answers the question.
- "1"
- "a"
- "b"
- "An error will occur"
Dictionary Methods and Operations
Similar to lists and strings in Python, dictionaries also have built-in functions for insertion, deletion, updating elements, and other operations that need to be performed on dictionaries.
To get the total length of the dictionary, we use the len()
function. The total length of the dictionary is the number of items (or key-value pairs) in the dictionary. We saw this in strings and lists as well.
1student = {"ID": "01", "name":"Patrick", "age":18, "major":"Biology"}
2
3print(len(student))
To add new items in a dictionary, there is no built-in method. Rather, they are directly assigned by specifying the key name and its value with the dictionary as shown in the code block below.
1student = {"ID": "01", "name":"Patrick", "age":18, "major":"Biology"}
2
3student["section"] = "A"
4
5print(student) #prints the updated dictionary
Another important operation in dictionaries is the removal of items. This is performed by using the del
keyword by specifying the key name of the key-value pair with the dictionary name that needs to be removed.
1student = {"ID": "01", "name":"Patrick", "age":18, "major":"Biology"}
2
3del student["section"]
4
5print(student) #prints the updated dictionary
You can also remove the entire contents (all key-value pairs) of the dictionary using the dict.clear()
method.
1student = {"ID": "01", "name":"Patrick", "age":18, "major":"Biology"}
2
3student.clear()
4
5print(student) #prints the updated dictionary
Some more commonly used dictionary methods are,
- To get all the items or key-value pairs from the dictionary,
dict.items()
is used. It returns the key-value pairs in the form of a tuple. - To get all the keys in a dictionary,
dict.keys()
is used. This returns all the keys in the form of a list. dict.get(key)
returns a value for the specified key. If the key is not in the dictionary, the method returns None. This is an alternate method (other than indexing) to get values for a key in the dictionary.
Build your intuition. Fill in the missing part by typing it in.
What is the output of following code?
1d = {"Peter": 12}
2for i, j in d.items():
3 print(i, j)
Write the missing line below.
Summary
In this lesson, we learned about a new method to store data during the execution of a program. Variables, lists, and dictionaries are all different methods of storing data. Depending on the situation, these different data storage elements (or data structures) are used to store and manipulate data.
One Pager Cheat Sheet
- We will learn about dictionaries, a data structure in Python which uses
hashing
to store data inkey-value
pairs, allowing us to categorize data easily. - Dictionaries allow for the storage and retrieval of data in the form of
key-value
pairs, wherekeys
are used asindices
. - Dictionaries in Python are ordered collections of
immutable
key-value pairs, with no duplicate keys allowed. - No, a dictionary cannot have two same keys, but two same values
are allowed
. - Dictionaries have built-in methods for insertion, deletion, and other operations, as well as an
len()
function to get the total length of the dictionary. - The code iterates through the items of a dictionary using
dict.items()
and prints them in the form ofkey, value
, yielding the output "Peter 12". - We learned about different
data structures
, such as variables, lists and dictionaries, to store and manipulate data during the execution of a program.