What is a dictionary in Python (Explained With Examples)

To simply explain python dictionaries, think of them as a way to store data in a set of key-value pairs, with each key being unique.

The reason why we want to store data in dictionaries is due to its ability to quickly get values from calling the “Key”.

How do python dictionaries work?

As python dictionaries get values from calling a “key” it’s known as an associative array, meaning it associates values with the key that is being called, hence the word “associative”.

To create a python dictionary we have to use curly braces and a semicolon to separate the key and value pairs.


There are a couple of ways to make a python dictionary, but here is what a basic one should look like.

PeopleAge = 
{'James' : 17,
'Robby' : 10,
'Cassandra' : 45,
'Kevin' : 23,
'Frank' : 77}

print(PeopleAge['James'])print(PeopleAge)

Basically what is happening in the dictionary is storing a string format of the names and values of the age, and each Key: Value pair is separated by a comma.

To print the entire values that are stored in the dictionary we can call print(PeopleAge) and to identify James age we can simply call Print(PeopleAge[‘James’]).

Here is the output:

17

{'James': 17, 'Robby': 10, 'Cassandra': 45, 'Kevin': 23, 'Frank': 77}

To better understand the full utility of the dictionary function, let’s take a look at a few examples below.

Example: Adding dictionary values

As dictionaries are mutable objects, this means we can update our key-value pair, by modifying an existing entry or deleting an existing entry.

Let’s take a look at modifying our dictionary example below:

PeopleAge = {
'James' : 17,
'Robby' : 10,
'Cassandra' : 45,
'Kevin' : 23,
'Frank' : 77
}

PeopleAge['James'] = 22 # Update existing entry
PeopleAge['Jennifer'] = 55 # Add new entry

print(PeopleAge['James'])
print(PeopleAge)

To update the values in a dictionary is as simple as calling the key and assigning a new value. So to update James age it is simply just assigning a new value by calling PeopleAge[‘James’] = 22.

Adding a new value is also very simple since dictionaries only deal with unique keys. If the key value is not detected in the dictionary, the dictionary will create a new key-value pair.

To add a new key-value pair in our dictionary we can just call PeopleAge[‘Jennifer’] = 55.

As shown below, the output of James age has been updated, Jennifer’s key-value pair.

22

{'James': 22, 'Robby': 10, 'Cassandra': 45, 'Kevin': 23, 'Frank': 77, 'Jennifer': 55}

Example: Deleting dictionary elements

Since dictionaries are immutable, we can also delete any key-value pairs or the entire dictionary by using the del statement.

Take a look at the example below:

PeopleAge = {
'James' : 22,
'Robby' : 10,
'Cassandra' : 45,
'Kevin' : 23,
'Frank' : 77,
'Jennifer': 55
}

del PeopleAge['Jennifer'] # remove entry with key Jennifer
print(PeopleAge)

del PeopleAge # delete entire dictionary
print(PeopleAge)

To delete the values in our dictionary we can simply call the del function with the key we want to delete. In our case, let’s delete Jennifer by calling del PeopleAge[‘Jennifer’]

Similarly, to delete our entire dictionary we can simply call del PeopleAge.

This is what our output should look like:

{'James': 22, 'Robby': 10, 'Cassandra': 45, 'Kevin': 23, 'Frank': 77}

Traceback (most recent call last):
  File "<string>", line 14, in <module>
NameError: name 'PeopleAge' is not defined

As you can see, Jennifer’s key-value pair has been deleted, and since we deleted our entire dictionary python has thrown an exception which indicates the PeopleAge dictionary does not exist.

Frequently Asked Questions

What are Python Dictionaries?

A dictionary is an unordered and mutable Python container that stores mappings of unique keys to values. Python dictionaries are used so they can help store data in a set of key-value pairs.

Are Dictionaries better than lists Python?

Yes, the reason why dictionaries are better is they use a lookup however lists is an iteration. This means the list needs to traverse through each individual value to find the correct match.