Dictionary Comprehension in Python With Examples

This article briefly covers the dictionary comprehension in Python. A Python dictionary is a very useful feature of Python Language. It serves as a mapping between keys to their values.

Sometimes, assigning values to specific keys might get too tedious for a programmer. Therefore Python has a provision of Dictionary Comprehension to save user’s precious time.

If you already know List Comprehension, this article is a piece of cake.

Basic Python Dictionary Comprehension

Output:

There are a few elements involved in as single line of code. Let us walk through each one of them.

  • Variableodd – The dictionary variable that stores all the information.
  • Keykey – The keys for the dictionary.
  • Valuekey % 2 – The values corresponding to the keys.
  • Iteratorfor key – The job of the iterator is to store relevant value for each iteration.
  • Iterablerange(1, 10) – The iterable is responsible for the functioning of the loop and granting value to the iterator.

Some illustrative examples

There is a need to go through more examples, to become capable of using dictionary comprehension in day-to-day coding.

Convert Temperature

Output:

In the above example, the dictionary comprehension goes through the items of another dictionary and assigns key and value pairs according to the formula designed.


Presence of elements

Output:

This conversion of list to a dictionary reduces the time complexity for any look-up queries. The check for the presence of elements can be done in O(1) complexity instead of O(N).


Inverting a Dictionary

Output:

One of the most useful implementation of dictionary comprehension is inverting a Python dictionary. The above code switches the key and value pairs.


Conditional Dictionary Comprehension

There is yet another element that can be added to the dictionary comprehension – Conditional Statement. These statements can be used to filter dictionary contents according to keys or values.

Output:

The conditional statements are placed after the iteration part if the set of values and keys are fixed in the dictionary made by dictionary comprehension.

In case, there is an if-else condition related to the values of the dictionary, then the conditions must be placed before the iteration segment.

Output:


Multiple conditional statements

Output:

In the above code snippet, there are two filters present, one filters hot places, while the other one filters places that start with 'M'.


Nested dictionary comprehension

There can be complex dictionaries enacted by nesting multiple dictionary comprehensions.

Creating multiplication tables

Output:

Nested Dictionary Comprehensions lack readability and therefore should be avoided in Python.


Conclusion

Dictionary Comprehension in Python is a great way to reduce the size of the program, but it may be lead to confusion during code-review. Therefore it is up to your discretion to take home the knowledge imparted by this article.

Thank you for reading!

By admin

Leave a Reply