Python Dictionaries

The Fellow
Oct 3, 2018 · 9 min read
“apple and pear in clear glass display” by Randy Fath on Unsplash

Data structures are basically containers that store data in predefined layouts, optimized for certain operations — like apples in a box, ready for picking😉.

The Python programming language natively implements a number of data structures. Lists, tuples, sets, dictionaries are but some of them. We will be looking at the dictionary data type in subsequent sections.

What are dictionaries ?

key-value mapping

A dictionary in python is a mapping object that maps keys to values, where the keys are unique within a collection and the values can hold any arbitrary value. In addition to being unique, keys are also required to be hashable.

An object is said to be hashable if it has a hash value (implemented by a __hash__() method) that does not change during the object’s lifetime. Most commonly, we use immutable data types such as strings, integers, and tuples (only if they contain similarly immutable types) as dictionary keys.

A dictionary’s data is always enclosed by a pair of curly braces { }, and would normally look like this:

my_dict = {"first_name": "John", "last_name":"Snow", "age":16, "gender":"Male"}

We have created a dictionary named my_dict where each key-value pair is separated by a full colon, with the key-value pairs as:

Typically dictionaries store associative data, i.e data that is related. Examples of such data include the attributes of an object, SQL query results and csv-formatted information. Throughout this article, we will be using dictionaries to store job listing details from Kaggle.


Dictionaries are an implementation of Associative Arrays. All Associative arrays have a structure of (key, value) pairs, where each key is unique for every collection. Other languages also have similar implementations, such as:

Unlike sequenced data types like lists and tuples, where indexing is achieved using positional indices, dictionaries are indexed using their keys. Therefore, individual values can be accessed using these keys.

Dictionary Operations

1. Creation

empty_dict = {}

In the line above, we have created an empty dictionary named empty_dict.

>>> job1 = {"title":"Production Manager",
"location":"Rest of Kenya",
"job_type":"Full Time",
"employer":"The African Talent Company (TATC)",

We just created a dictionary with the keys title,location, job_type, employer, category and assigned it to the variable job1.

Creating dictionary with list of key-value tuples

We passed a sequence, in this case a list of key-value tuples, to the dict() constructor to create our dictionary, and assigned it to the variable job2.

Creating dictionary with dict( )

Here, we created a dictionary using named arguments. The keys are the argument names, while the values are the argument values. It is however important to note that this method is only suitable when our keys are just simple strings.

2. Accessing Items

As we mentioned earlier on, dictionaries are indexed using their keys.
To access a particular value in a dictionary we use the indexing operator (key inside square brackets). However, to use this method, we need to make sure the key we intend to retrieve exists, lest we get a KeyError. Checking for availability of a key is as easy as using the in operator.

Accessing items

Here, we use get() to access the title and salary.

However, job2 doesn't have a salary key so the return value is None. Adding a second argument, to get() now gives us 5000 instead of None.

3. Modification

Dictionaries can be modified directly using the keys or using the update() method. update() takes in a dictionary with the key-value pairs to be modified or added. For our demonstration, let's:

Updating dictionaries

To add a new entry, we use syntax similar to indexing. If the key exists, then the value will be modified, however, if the key doesn’t exist, a new entry will be created with the specified key and value.

A particularly nice use case for update() is when we need to merge two dictionaries. Say we have another dictionary extra_info containing extra fields for a job, and we would like to merge this with job2.

Merging dictionaries

4. Deletion

We can now remove the just created salary entry from job2, and remove everything from job1.


To remove the entries associated with the salary and available keys from job2, we use the del keyword. Now if we go ahead and print job2, the salary and available entries are gone.

Removing all items from job1 entails using the clear() method, which leaves us with an empty dictionary. If we don't need a dictionary anymore, say job1, we use the del keyword to delete it. Now if we try printing job1 we'll get a NameErrorsince job1 is no longer defined.

6. Iteration

A dictionary by itself is an iterable of its keys. Moreover, we can iterate through dictionaries in 3 different ways:

But why would we need to iterate over a dictionary?

Our dataset has about 860 listings, suppose we wanted to display the properties of all these on our website, it wouldn’t make sense to write the same markup 860 times. It would be efficient to dynamically render the data using a loop.

Let’s iterate over job2 using a for-loop using all the three methods. Furthermore we'll use the csv module to read our csv-formatted data in to a list of dictionaries, then we'll iterate through all the dictionaries and print out the keys and values.

Dictionary Iteration

7. Sorting

Borrowing from our description of dictionaries earlier, this data type is meant to be unordered, and doesn’t come with the sorting functionality baked in. Calling the sorted() function and passing it a dictionary only returns a list of the keys in a sorted order, since the dictionary is an iterable of its keys.

If we use the items() iterable we could sort the items of our dictionary as we please. However, this doesn't give us our original dictionary, but a list of key-value tuples in a sorted order.

Say we wanted to display the job details in the above example in alphabetical order, We would need to alter our iteration to give sorted results. Lets walk through the example again an see how we would achieve that functionality.


Other Methods

Dictionaries have other methods that could be used on demand. To read up further on these, please consult the python documentation. Here are some other useful methods:

Speeding Up your Code

Dictionary unpacking can greatly speed up our code. It involves destructuring a dictionary into individual keyword arguments with values.
This is especially useful for cases that involve supplying multiple keyword arguments, for example in function calls.
To implement this functionality we use the iterable unpacking operator (**).

What if we needed Job objects to work with, instead of dictionaries? We shouldn't have to do some heavy lifting to get our data reorganized in to objects.
Let's see how we could translate our dictionaries into objects, by again tweaking our previous code.

Dictionary Unpacking

8. Anti-patterns: Wrong usage

Compared to lists and tuples, dictionaries take up more space in memory, since they need to store both the key and value, as opposed to just values.


We have a variable key_i_need containing the key we want to search for. We have used a for loop to traverse the collection, comparing the key at each step with our variable. If we get a match, we assign that key's value to the variable target.
This is the wrong approach. We should instead use get(), and pass it the desired key.

Performance Trade-offs

Dictionary operations are heavily optimized in python, especially since they’re also extensively used within the language.
For instance, members of a class are internally stored in dictionaries.

Most dictionary operations have a time complexity of O(1) — implying that the operations run in constant time relative to the size of the dictionary. This simply means that the operation only runs once irregardless of the dictionary size.
Creating a dictionary runs in a linear time of O(N), where “N” is the number of key-value pairs.
Similarly, all iterations run in O(N) since the loop has to run N times.

Dictionary Operations —


Dictionaries come in very handy for regular python usage. They are suitable for use with unordered data that relies on relations. Caution should however be exercised to ensure we do not use dictionaries in the wrong way and end up slowing down execution of our code. For further reading please refer to the official python documentation on mapping types.

Python Pandemonium

A place to read and write about all things Python. If you want to become a writer for this publication then let me know.

The Fellow

Written by

Software Dev | Pythonista | Data Eng. Enthusiast|

Python Pandemonium

A place to read and write about all things Python. If you want to become a writer for this publication then let me know.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade