Dictionary Comprehension in Python

In this tutorial, we will discuss about dictionary comprehension in Python, and we will learn how we can use it and also understand some examples.

In Python, dictionaries are the data types in which the users can store the data in a pair of key/value.

Example:

What is Dictionary Comprehension?

Dictionary comprehension is the method used for transferring one dictionary into another dictionary. Throughout the process of transferring the dictionary into another, the user can also include the data of the original dictionary into the new dictionary, and each data can be transferred as per need.

Just like list comprehension, Python also allows the user to perform dictionary comprehensions. The user can create the dictionary by using the simple expression of the built-in method.

The dictionary comprehension is created in the following form:

Example 1: (Method 1)

Output:

user_Dict: {'p': 56, 'q': 67, 'r': 43, 's': 12, 't': 6}

Example 2: (Method 2)

Output:

user_Dict_1: {'j': 34, 'k': 54, 'l': 13, 'm': 76, 'n': 98, 'o': 74}

The user can also create the dictionary from a list by using comprehension.

Example 3: (Method 3)

Output:

user_Dict2: {56: 3136, 67: 4489, 43: 1849, 12: 144, 6: 36}

Example 4: (Method 4)

Output:

user_Dict: {'J': 'JJJ', 'A': 'aaa', 'V': 'vvv', 'T': 'ttt', 'P': 'ppp', 'O': 'ooo', 'I': 'iii', 'N': 'nnn', ' ': '   ', 'S': 'sss', 'H': 'hhh', 'E': 'eee', 'B': 'bbb', 'L': 'lll', 'R': 'rrr', 'G': 'ggg', 'W': 'WWW'}

The user can also create Dictionary comprehension by using if and else statements.

Example 5: (Method 5)

Output:

user_dict4: {0: 0, 2: 8, 4: 64, 6: 216, 8: 512, 10: 1000, 12: 1728, 14: 2744, 16: 4096, 18: 5832, 20: 8000, 22: 10648, 24: 13824, 26: 17576, 28: 21952}

Example 6: (Method 6)

Output:

user_dict5: {3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100, 11: 121, 12: 144, 13: 169, 14: 196, 15: 225, 16: 256, 17: 289, 18: 324, 19: 361}

How to use Dictionary Comprehension

Let's understand the following example.

Example -

In the above example, the user has used dictionary comprehension for converting the dollar value into pound value.

Output:

old_price_1: {'milk per litter': 2.12, 'coffee per cup': 3.6, 'bread per packet': 1.51}
new_price_1:  {'milk per litter': 1.5052, 'coffee per cup': 2.556, 'bread per packet': 1.0721}

Conclusion

In this tutorial, we have explained the different methods of creating dictionary comprehension in Python by using different parameters and methods. We also showed an example of how the user can use dictionary comprehension for converting the dollar value into pound value.






Latest Courses