List Comprehension and Dictionary Comprehension

Sandhya Krishnan
Geek Culture
Published in
4 min readMar 24, 2023

--

List comprehension

List comprehension is a very effective way to create an entirely new list or list based on the values of an existing list.

If we want to create a new list of integers from 0 to 10, we have to write a for loop to append each integer as below.

mylist = []
for i in range(10):
mylist.append(i)
print("My New List Is : ", mylist)
My New List Is :  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Whereas if we use list comprehension we can write the code in one single line. The list comprehension format is

To create a range of integers from 0 to 10, we can use list compression as below and assign to it mylist.

#mylist = [new item for item in iteratable]
mylist = [i for i in range(10)]
print("My New List With List Comprehension : ", mylist)
My New List With List Comprehension :  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

If we want to create a new list based on any condition, we can use the list comprehension with the format:

For example, we have to create a new list of even integers from the above list, it can be done by using the below formula.

#new_list = [expression for element in old_list if condition]
new_list = [element for element in mylist if element%2 == 0]
print("New List of Even Integers : ", new_list)
New List of Even Integers :  [0, 2, 4, 6, 8]

Here in one line of code, we can create a new list of even integers.

Dictionary Comprehension

A dictionary is a collection of key-value pairs, where each key is unique and mapped to a corresponding value. Dictionaries are denoted by curly braces { } and each key-value pair is separated by a colon.

An example of a dictionary is {‘Apple’: 285, ‘Mangoes’: 231, ‘Banana’: 276, ‘Strawberry’: 237, ‘Pineapple’: 270}, where fruits are key and integers are values.

Dictionary comprehension, like list comprehension, is a very effective method to create a dictionary from a list or from an existing dictionary based on the values.

If I have a fruit list If I want to create a dictionary with the key as fruit_list element and value a random integer between 200 and 300, then I can create it without dictionary comprehension as below.

fruit_list = ['Apple', 'Mangoes', 'Banana', 'Strawberry', 'Pineapple']
import random

fruit_list = ['Apple', 'Mangoes', 'Banana', 'Strawberry', 'Pineapple']
fruit_dict = {}

for item in fruit_list:
fruit_dict[item] = random.randint(200, 300)

print("fruit_dict is : ", fruit_dict)
fruit_dict is :  {'Apple': 255, 'Mangoes': 276, 'Banana': 296, 'Strawberry': 256, 'Pineapple': 275}

But when we use dictionary comprehension, we can replace the for loop with one line of code in the below format.

import random

fruit_list = ['Apple', 'Mangoes', 'Banana', 'Strawberry', 'Pineapple']

#fruit_dict = {key:value for element in list}
fruit_dict = {element:random.randint(200,300) for element in fruit_list}
print("fruit_dict with dict comprehension is : ", fruit_dict)
fruit_dict with dict comprehension is :  {'Apple': 276, 'Mangoes': 210, 'Banana': 298, 'Strawberry': 249, 'Pineapple': 297}

Note the value for dictionary keys will be different from the previous one, as the value is created randomly.

If we want to create a new dictionary from the above fruit_dict, if fruit values are above 250, without dictionary comprehension we can do with the below code.

fruit_dict = {
'Apple': 276,
'Mangoes': 210,
'Banana': 298,
'Strawberry': 249,
'Pineapple': 297
}

new_dict = {}
for key, value in fruit_dict.items():
if value > 250:
new_dict[key] = value

print(new_dict)
{'Apple': 276, 'Banana': 298, 'Pineapple': 297}

Here also by using dictionary comprehension, we can remove the for loop, and the syntax for dictionary comprehension for conditions will be as below.

fruit_dict = {
'Apple': 276,
'Mangoes': 210,
'Banana': 298,
'Strawberry': 249,
'Pineapple': 297
}

new_dict = {key:value for (key, value) in fruit_dict.items() if value > 250}

print("new_dict with dict comprehension is :\n", new_dict)
new_dict with dict comprehension is :
{'Apple': 276, 'Banana': 298, 'Pineapple': 297}

Also, we can create new values or new keys to the new dictionary using dictionary comprehension. For example, if we want to change the fruits to upper case and add 100 to the value if the fruit is Mangoes or Strawberry.

fruit_dict = {
'Apple': 276,
'Mangoes': 210,
'Banana': 298,
'Strawberry': 249,
'Pineapple': 297
}

new_dict = {key.upper():value + 100 for (key, value) in fruit_dict.items() if key == 'Mangoes' or key == 'Strawberry'}

print("new_dict is ", new_dict)
new_dict is  {'MANGOES': 310, 'STRAWBERRY': 349}

So we can conclude list and dictionary comprehension, are very effective and efficient methods.

--

--