[NEW] Basic gets a boost: Announcing major upgrades to our free AI code assistant
Home / Blog /
The ultimate guide to Python Dictionary comprehension
//

The ultimate guide to Python Dictionary comprehension

//
Tabnine Team /
5 minutes /
July 21, 2021

Python dictionaries — or dict in Python — is a way of storing elements similar to a Python list. However, rather than accessing elements through an index, accessibility is achieved through a pre-assigned fixed key. In a way, a dictionary is like a mini on-site database that can be queried using a “key-value” pair. This method of structuring data lets you create more complex structures and may be more suitable to your needs than a simple, flat, and potentially mutable list.

Dictionaries are something that comes up in spaces like data science, due to their ability to hold multiple dimensions and nested data. In this tutorial, we will go over the following:

  • what a Python dictionary is and how to effectively use it
  • what Python dictionary comprehension is and why it is an important alternative to loops and lambdas

Without further ado, let’s get started.

What is a Python dictionary and how to use one

A Python dictionary is a collection of items that’s accessible by a specific key rather than an index. This key can be one of the following types:

  • integer
  • floating point
  • number
  • string
  • tuple
  • fronzensets

This is because a Python dictionary key needs to be hashable. Hashing is a process of putting a specific variable through a type of function called a “hash function” to return a unique output.

Here is an example of how to initialize a Python dictionary:

 a = {'Luxembourg': 'Western Europe', 'Norway': 'Northern Europe', 'United States': 'North America', 'Ireland': 'Northern Europe'}

To add to the Python dictionary:

 a['Denmark'] = 'Northern Europe'

Now, if we print it out:

 print(a['Luxembourg'])

This will return:

 Western Europe

However, if it was a list, we’d use the following syntax:

 print(a[0])

But this will not work and will return a KeyError. This is because the key 0 doesn’t exist. Querying Python dictionaries is done through the key rather than an index.

Each item in the dictionary can have any data type and isn’t confined to a string as per the example above. It is also good to note that the key has to be unique in a Python dictionary. This means that no duplicates are allowed. However, if there are duplicate keys, Python will not throw an error. Instead, the last instance of the key will be the valid value.

For example:

 a = {'Luxembourg': 'Western Europe', 'Norway': 'Northern Europe', 'United States': 'North America', 'Ireland': 'Northern Europe', 'Luxembourg': 'Luxembourg City'}

So when you run print(a['Luxembourg']), the returned result will be Luxembourg City rather than Western Europe.

And that’s basically creating a Python dictionary in a nutshell.

What is Python Dictionary Comprehension?

Dictionary comprehension is a process of transforming one dictionary into another. The items in the original dictionary can be conditionally included and each item transformed as necessary.

In a way, it’s like copying your original dictionary and doing what you need to do with the values before your final dictionary is set. To do this, you will need to be able to access the key and the value within each dictionary object. This is achievable using .keys() and .values().

.keys() will return a list of keys contained in your dictionary. For example:

 a = {'Luxembourg': 'Western Europe', 'Norway': 'Northern Europe', 'United States': 'North America', 'Ireland': 'Northern Europe'}
 ​
 a.keys()

This will return:

 dict_keys(['Luxembourg', 'Norway', 'United States', 'Ireland'])

The same is applied to .values(). Here is an example of what it looks like:

 a.values()

This will return:

 dict_values(['Western Europe', 'Northern Europe', 'North America', 'Northern Europe'])

Now that you’ve created these lists, you can now pair them up using items(). This will give you access to each key-value pair. Here is an example:

 a.items()

This will return:

 dict_items([('Luxembourg','Western Europe'), ('Norway','Northern Europe'), ('United States','North America'), ('Ireland','Northern Europe')])

Using keys(), values() and items() sets you up with the ability to create dictionary comprehensions in Python. The general syntax structure for creating a new dictionary is as follows:

 new_dictionary_name_here = {key: value for (key,value) in origianl_dictionary.items()}

For a direct copy of the dictionary through Python dictionary comprehension, here is an example of the syntax:

 a_copy = {k:v for (k,v) in a.items()}

The first v is representative of any transformations you may want your values to be. You can also make transformations to the key as well by modifying the first instance of the k.

For example:

 alpha_dictionary = {'z': 26, 'y': 25, 'x': 24, 'w': 23}
 double_dictionary = {k*2:v*2 for (k,v) in alpha_dictionary.items()}

So when we use print(double_dictionary), we will get the following results:

 {'zz': 52, 'yy': 50, 'xx': 48, 'ww': 46}

Why Use Dictionary Comprehension?

As shown in the process above, we don’t have to loop or use lambda functions in order to create copies of a dictionary. During the copying process, dictionary comprehension also lets us create transformations easily.

The thing with loops is that they are used to repeat a block of instructions for a given number of iterations. But things can get complicated for nested loops, which can create confusion and unnecessary complexity.

Dictionary comprehensions are a better alternative to writing loops due to simplification of the ‘looping’ process and therefore readability. For example, rather than writing something like this:

 alpha_dictionary = {'z': 26, 'y': 25, 'x': 24, 'w': 23}
 double_dictionary = {}
 ​
 # Add values to `alpha_dictionary` using for loop
 for n in numbers:
     if n%2==0:
         new_dict_for[n] = n**2
 ​
 print(double_dictionary)

You can simply write the following using Python dictionary comprehension:

 alpha_dictionary = {'z': 26, 'y': 25, 'x': 24, 'w': 23}
 ​
 double_dictionary = {n:n**2 for n in numbers if n%2 == 0}
 ​
 print(double_dictionary)

Python dictionary comprehension is also a good alternative to lambda functions. A lambda function is a way of creating small anonymous functions. They are considered throwaway functions and are nameless. Their existence only lasts as long as they are needed. lambda functions tend to also include other functions such as filter(), map(), and reduce() to achieve its desired outcome.

For example, let’s create a new dictionary using lambda and map() function.

 fahrenheit = {'t1':0, 't2':10, 't3':20, 't4':30}
 ​
 celsius = list(map(lambda x: (float(5)/9)*(x-32), fahrenheit.values()))
 ​
 celsius_dict = dict(zip(fahrenheit.keys(), celsius))

However, converting Fahrenheit to Celsius is much simpler using a dictionary comprehension. Here is the refactored code:

 fahrenheit = {'t1':0, 't2':10, 't3':20, 't4':30}
 celsius = {k:(float(5)/9)*(v-32) for (k,v) in fahrenheit.items()}

Overall, there are less lines involved with a dictionary comprehension and informs the developer exactly what the expected end result is to be — another dictionary with the values translated and transformed from the original dictionary.

This is the barebones basics of python dictionary comprehension. The next step is to explore conditions, such as if conditions and using multiple if conditions. Beyond this is nested dictionary comprehension — which poses its challenges due to the nested nature. This is where loops can come into play and help.

Overall, dictionary comprehension is the easiest way to work with sets of data concisely and succinctly. It’s not hard once you get your head around the idea and the techniques above are more than enough to get you started.