How to create and work with Python dictionaries

What is a Python dictionary? How can you use it to store information and how can you read from it? Is a python dictionary read-only or can it be modified? After reading this tutorial, you have learned how to use Python dictionaries

A dictionary in your book shelf consist of words and their explanations. It usually couples words to other things like definitions, etymology or translations to different languages. The following example shows words and their meanings:

Word Meaning
Berlin City in Germany
The Beatles One of the best bands in the world
Hobbit A mystical creature, created by Tolkien

A dictionary in programming languages also has a word (called a key) and another value that couples with it.

Example

Imagine an app that needs to store the user that is logged in. This user has a name and email address:

Key Value
Name Vera
Email Address vera@abc.com

Python could store them as follows:

name = "Vera"
email_address = "vera@abc.com"

You could write a function that prints the logged-in user to the screen. The signature for this function could look like this:

def show_user(name, email_address):
    print(name)
    print(email_address)

As the system grows, the user might get more properties like address, last logged in date, etc. Before you know it, you are passing all these values around in your app. And every time a new property is being added, you need to change all the functions that deals with users.

Group that information into a dictionary

It would be better to group all the user information and a dictionary is perfect for this purpose. Remember that a dictionary is a collection of keys and their values. A key and value are called a key / value pair.

Creating a python dictionary

Creating a dictionary in Python is easy. The collection of key / value pairs is defined as follows:

user = {
    "name": "Vera",
    "email_address": "vera@abc.com"
}

The collection is enclosed with curly braces and every key/value pair is separated by commas.

The python print command is capable of printing dictionary data. For example:

user = {
    "name": "Vera",
    "email_address": "vera@abc.com"
}

print(user)

Would output:

{'name': 'Vera', 'email_address': 'vera@abc.com'}

Having grouped name and email_address into a dictionary, we can already start cleaning up the show_user function. It will now take just 1 argument, regardless of how many user properties there are:

def show_user(user):
    print("logged-in user", user)

What we have achieved here is that the show_user function does not need to change when additional user properties are added to the dictionary

Read one value from the dictionary

Let’s say you want to know the email address of the logged-in user. For this you need to read the email_address value. Here is how you do that:

email_address = user["email_address"]
print(email_address)

output:

vera@abc.com

Changing values

Python dictionaries are mutable and that means we can change its values! Let’s say Veras email address is updated and you want to store this in the dictionary:

user["email_address"] = "vera@xyz.com"
print(user)

output:

{'name': 'Vera', 'email_address': 'vera@xyz.com'}

Check if a dictionary has a key

We know that we can create, read from and write to a dictionary. But what if we want to know if a dictionary has a certain key? We can do that easily with Pythons in keyword:

user = {
    "name": "Vera",
    "email_address": "vera@abc.com"
}

if "email_address" in user:
    print("The user has an email address")
else:
    print("The user does not have an email address")

if "birth_date" in user:
    print("The user has a birth date")
else:
    print("The user does not have a birth date")

output:

The user has an email address
The user does not have a birth date

Summary

You have learned how to create a dictionary and how to read from it and write to it. By using Pythons it keyword, you can check if a key is in the dictionary. These are the basic things you can do with dictionaries and there’s a lot more to explore. I hope this tutorial has given you an idea of what a dictionary is and how to use them in your projects.

Written by Loek van den Ouweland on October 04, 2019. Questions regarding this artice? You can send them to the address below.
By using this site, you acknowledge that you have read and understand our Cookie and Privacy Policy.