Python Dictionary Comprehension: A Concise Guide

Python dictionary comprehension is a concise and expressive way to create dictionaries.

It allows you to construct dictionaries using a single line of code, providing a more compact and readable alternative to traditional loops and conditional statements.

In this guide, we'll explore the syntax, usage, and examples of dictionary comprehension in Python.

Syntax of Dictionary Comprehension:

The syntax of a dictionary comprehension is as follows:

{key_expression: value_expression for item in iterable if condition}

Basic Dictionary Comprehension Example:

Let's start with a simple example where we create a dictionary of squares for numbers from 1 to 5.

# Using dictionary comprehension to create a dictionary of squares
squares_dict = {x: x**2 for x in range(1, 6)}

# Display the result
print(squares_dict)

The output will be:

{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

In this example, the dictionary comprehension generates key-value pairs where each key is a number from 1 to 5, and the corresponding value is the square of that number.

Dictionary Comprehension with Condition:

You can include a condition to filter items in the iterable. In the following example, we create a dictionary containing only even numbers and their squares.

# Using dictionary comprehension with a condition
even_squares_dict = {x: x**2 for x in range(1, 6) if x % 2 == 0}

# Display the result
print(even_squares_dict)

The output will be:

{2: 4, 4: 16}

Here, the condition if x % 2 == 0 ensures that only even numbers are included in the dictionary.

Dictionary Comprehension with Zip:

You can use the zip function to combine two iterables and create key-value pairs.

In the following example, we create a dictionary from two lists representing days and their corresponding temperatures.

# Using dictionary comprehension with zip
days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
temperatures = [22, 25, 24, 26, 23]

temperature_dict = {day: temp for day, temp in zip(days, temperatures)}

# Display the result
print(temperature_dict)

The output will be:

{'Monday': 22, 'Tuesday': 25, 'Wednesday': 24, 'Thursday': 26, 'Friday': 23}

Here, the zip(days, temperatures) combines the two lists into pairs, and the dictionary comprehension creates key-value pairs from those pairs.

Nested Dictionary Comprehension:

You can also use nested dictionary comprehension to create dictionaries within dictionaries.

In the following example, we create a nested dictionary representing multiplication tables.

# Using nested dictionary comprehension
size = 3
multiplication_table = {i: {j: i * j for j in range(1, size + 1)} for i in range(1, size + 1)}

# Display the result
print(multiplication_table)

The output will be:

{1: {1: 1, 2: 2, 3: 3}, 2: {1: 2, 2: 4, 3: 6}, 3: {1: 3, 2: 6, 3: 9}}

In this example, the outer dictionary represents the multiplicand, and each inner dictionary represents the multiplication with various multipliers.

Conclusion:

Python dictionary comprehension is a powerful and concise tool for creating dictionaries in a readable and expressive manner.

It allows you to construct dictionaries with less boilerplate code compared to traditional loops and conditional statements.

As you become familiar with dictionary comprehension, you'll find it to be a valuable addition to your Python programming arsenal, enabling you to write more elegant and efficient code. Happy coding!