| by Arround The Web | No comments

Dictionary Comprehension Python

Python provides a concise and effective way to create dictionaries referred to as “Dictionary Comprehension“. This feature allows you to create dictionaries with a single line of code and improve the readability and functionality of your code. This blog explores the concept of Python dictionary comprehension utilizing numerous examples.

What is “Dictionary Comprehension” in Python?

“Dictionary Comprehension” is a compact and elegant way to create dictionaries based on an existing iterable object or other dictionaries. It allows you to define the “key-value” pairs and generate a dictionary using a concise syntax. This approach is an alternative to the traditional methods of dictionary creation, such as using “loops” or the “dict()” constructor.

Syntax

{key_expression: value_expression for item in iterable}

 

In the above syntax:

  • The “key_expression” represents the expression that defines the key for each item in the iterable.
  • The “value_expression” corresponds to the expression that defines the corresponding value.
  • The “item” variable is utilized to loop over/through the iterable.

Return Value

The return value of “Dictionary Comprehension” is a new dictionary containing the “key-value” pairs generated based on the provided expressions and iterable.

Example 1: Creating a Dictionary Using “Dictionary Comprehension”

In the below code, the “Dictionary Comprehension” approach is used to create a dictionary:

value1 = ['Joseph', 'John', 'Anna', 'Lily']
output = {name: len(name) for name in value1}
print(output)

 

In the above code:

  • The list named “value1” is initialized.
  • The “for” loop is used to iterate over each element in the given list and create a “key-value” pair where the key is the “name” and the value is the “length” of the name.
  • The resulting dictionary contains the desired key-value pairs.

Output

As seen, the corresponding length i.e., the “value” of the “keys” is retrieved accordingly.

Example 2: Modifying Values in the Existing Dictionary Using “Dictionary Comprehension”

“Dictionary Comprehension” can also be used to modify values in an existing dictionary. Here is an example code:

value = {'Alice': 80, 'Bob': 90, 'Charlie': 75}
output = {student: score + 5 for student, score in value.items()}
print(output)

 

In the above example, iterate over each “key-value” pair in the dictionary named “value” and create a new “key-value” pair where the key remains the same, whereas the value is incremented by 5.

Output

According to the above outcome, the dictionary contains the modified scores i.e., “values”.

Conclusion

Dictionary Comprehension” is a powerful and concise way to create dictionaries in a Python program. It allows you to generate dictionaries with a single statement code and enhance your code’s readability and efficiency. This Python guide presented a complete guide on “Dictionary Comprehension” using numerous examples.

Share Button

Source: linuxhint.com

Leave a Reply