Python Dictionary with Examples


How to Use the Python Dictionary

In this tutorial, we'll learn what is a dictionary in Python, how to create a dictionary, add and remove items to and from a dictionary, iterate a dictionary, and count the number of elements in a dictionary.

What is a Dictionary in Python?

A dictionary is an unordered collection of items. While other collection data types have only values, a dictionary has keys and values pair.

Creating a Dictionary in Python

Dictionary literals are written within curly brackets { }.

The following example illustrates how to create dictionaries:

students = {
"101": "Student A",
"102": "Student B",
"103": "Student C"
}
students = {
1: "Student 1",
2: "Student 2",
3: "Student 3"
}
items = {
"sales_price": 1200.50,
"cost": 1100,
"item_name": "Item 001",
"quantity": 20
}

Accessing Items

To access the items of a dictionary, we use the key inside square brackets [] to get its value.

The following is an example that illustrates how to get a value of the item that has key "101":

students = {
"101": "Student A",
"102": "Student B"
}
student = students["101"]
print (student)

Result:

Student A

We also can use the get() method to get the same result.

students = {
"101": "Student A",
"102": "Student B"
}
student = students.get("102")
print (student)

Result:

Student B

Adding Items

We add an item to a dictionary by using a new key and assign a value to it.

The following is an example illustrates how to add a new item to the dictionary:

employees = {
1: "Employee A",
2: "Employee B",
}
employees[3] = "Employee C"
print (employees)

Result:

{1: 'Employee A', 2: 'Employee B', 3: 'Employee C'}

Removing Items

To remove the item at the specified key from a dictionary, we use the pop() method.

The following illustrates how to remove the item using the pop() method:

employees = {
1: "Employee A",
2: "Employee B",
}
employees[3] = "Employee C"
print (employees)
employees.pop(1)
print (employees)

Result:

{1: 'Employee A', 2: 'Employee B', 3: 'Employee C'}
{2: 'Employee B', 3: 'Employee C'}

We also can remove the item from a dictionary using the del keyword.

employees = {
1: "Employee A",
2: "Employee B"
}
del employees[1]
print (employees)

Result:

{2: 'Employee B'}

The del keyword can also delete the dictionary completely.

Let's take a look at the following example:

items= {
"unit_price": 1500,
"unit_cost": 2300,
"name": "Item 001"
}
del items

Now the items dictionary has been deleted, so it's no longer available.

Changing Values

We can change the value of a specific item by assigning a new value to the existing key as follows:

dictionary_name[key] = value

The following is an example illustrates how to change a value of the item in the dictionary:

students["name"] = "James"
students["id"] = "ID_00001"

Note that if the key does not exist, it is considered to be a new key and value pair.

Now, let's take a look at an example:

students = {
"name": "James"
}
print ("=== before ===")
print (students)
students["name"] = "John"
students["id"] = "ID_0001"
print ("=== after ===")
print (students)

Result:

=== before ===
{'name': 'James'}
=== after ===
{'name': 'John', 'id': 'ID_0001'}

As you can see, since the key name already exists, the value has changed from James to John. But in case of the key id, since it doesn't exist, it's considered to be a new item.

Check if Key Exists

To check if a specified key is present in a dictionary, we use the in keyword.

Let's take a look at an example:

employee = {"ID": "20002", "DateOfBirth": "2010-10-01"}
print ("Check if Name exist!")
if "Name" in employee:
print ("The key \"Name\" already exists in the dictionary!")
else:
print ("The key \"Name\" doesn't exist in the dictionary!")
print("====")
print ("Check if ID exists!")
if "ID" in employee:
print ("The key \"ID\" exists in the dictionary!")
else:
print ("The key \"ID\" doesn't exist in the dictionary!")

Result:

Check if Name exist!
The key "Name" doesn't exist in the dictionary!

====
Check if ID exists!
The key "ID" exists in the dictionary!

In the above example, we've used the IF statement. If you don't know how to use it, please take a look at the following link:

In this tutorial, we've learned what is a dictionary in Python, how to create a dictionary, add and remove items to and from a dictionary, iterate a dictionary, and count the number of elements in a dictionary.