Python | Dictionary Comprehension

Python | Dictionary Comprehension

Introduction: Python’s dictionary comprehension is an elegant way to create dictionaries from iterable objects such as lists, tuples, or even other dictionaries. It offers a simple syntax for generating dictionaries with ease and flexibility. In this article, we’ll explore the concept of dictionary comprehension in Python, explore its syntax, features, and demonstrate its usage through practical examples.

Understanding Dictionary Comprehension: This is similar to list comprehension, but instead of creating lists, it creates dictionaries. It allows you to create dictionaries by specifying key-value pairs and iterating over iterable objects. The resulting dictionary is created by applying an expression to each item in the iterable.

Syntax of Dictionary Comprehension: The basic syntax is as follows:

new_dict = {key_expr: value_expr for item in iterable if condition}

Here, key_expr and value_expr are expressions that define the key-value pairs of the dictionary. item represents each element in the iterable, and condition is an optional filtering condition.

Example 1: Basic Dictionary Comprehension:
Let’s start with a simple example to illustrate the usage of dictionary comprehension:

name_list = ["Ankit", "Rahul", "Biochemithon"]
name_length_dict = {name: len(name) for name in name_list}
print(name_length_dict)

Output:

{'Ankit': 5, 'Rahul': 5, 'Biochemithon': 12}

In this example, dictionary comprehension is used to create a dictionary where each name is the key, and its length is the value.

Example 2: Dictionary Comprehension with Conditional Filtering:
Dictionary comprehension can also include conditional filtering to include or exclude items based on certain conditions. Here’s an example:

numbers_list = [11, 12, 13, 14, 15]
even_numbers = {num: num ** 2 for num in numbers_list if num % 2 == 0}
print(even_numbers)

Output:

{12: 144, 14: 196}

In this example, dictionary comprehension is used to create a dictionary containing the squares of even numbers from the numbers list.

Example 3: Using Dictionary Comprehension with Strings:
Dictionary comprehension is not limited to numeric operations; it can also be used with strings. Here’s an example of creating a dictionary with string elements converted to uppercase:

words_list = ["apple", "banana", "cherry"]
uppercase_words = {word: word.upper() for word in words_list}
print(uppercase_words)

Output:

{'apple': 'APPLE', 'banana': 'BANANA', 'cherry': 'CHERRY'}

In this example, dictionary comprehension is used to create a dictionary where each word is the key, and its uppercase version is the value.

Example 4: Nested Dictionary Comprehension:
Dictionary comprehension can be nested to create nested dictionaries or perform more complex operations. Here’s an example of creating a nested dictionary using nested dictionary comprehension:

keys = ["a", "b", "c"]
values = [1, 2, 3]
nested_dict = {key: {value: value ** 2 for value in values} for key in keys}
print(nested_dict)

Output:

{'a': {1: 1, 2: 4, 3: 9}, 'b': {1: 1, 2: 4, 3: 9}, 'c': {1: 1, 2: 4, 3: 9}}

In this example, a nested dictionary is created where each key is associated with another dictionary containing the squares of values. For each key inner dictionary will be executed.

Conclusion: Dictionary comprehension is a good tool in Python for creating dictionaries with minimal code and maximum readability. Whether you’re creating dictionaries from lists, tuples, or other dictionaries, dictionary comprehension offers an easy solution for generating dictionaries in Python.

Leave a Reply

Your email address will not be published.