List and dictionary comprehension in python

In this tutorial, you are going to learn about the list and dictionary comprehension in python. Both of them will help us to reduce the length of the code that will save the memory and make the code effective. To have a good understanding of the topic, you should know about the list and dictionary first. I’ll give you a quick introduction to both of them so, that it becomes easy for you to understand the topic well.

The list is a data type in python which is mutable in nature (i.e., elements can be changed) and can have different types of data in it.
For example:  a=[10,20,30,40]             b=[10, 36.4, ‘a’, ‘HEY’]

# list 1
# to get the type of the variable

# list 2


<class 'list'>
[10, 20, 30, 40]
<class 'list'>
[10, 36.4, 'c', 'HEY']

A dictionary is a data type in python which is mutable in nature and have a set of key and value pair which is known as item and are separated by a colon(:).
For ex: a={11:’ab’, 12:’cd’, 13:’ef’}    b={‘a’:123, ‘s’:345}

# dictionary 1
a={11:'ab', 12:'cd', 13:'ef'} 

# dictionary 2
b={'a':123, 's':345}


<class 'dict'>
{11: 'ab', 12: 'cd', 13: 'ef'}
<class 'dict'>
{'a': 123, 's': 345}

List comprehension in Python

It provides a concise way to create a list.
result= [ value iteration filter]
* filter is optional.
The value can be anything, meaning which you want to evaluate like (x+2), (x*x), (x%2) and so on. Iteration means the for loop which will help to evaluate on the expression one by one. A filter is an optional one. It is used to filter or to choose the value to get the desired output. The result will be stored in a new list resulting from evaluating the expression.

Program without using list comprehension (lc):

  1. Make the empty list.
  2. Create the for loop with a range (1,6)
  3. Evaluate the expression and append it into the list.
  4. Print it to get the output.
# list
for s in range(1,6):


[2, 4, 6, 8, 10]

Program using lc without filter:

  1. The loop will evaluate first.
  2. The value of s goes into the expression part s*2.
  3. The result of the expression will store in the new list.
  4. The loop will continue until s=5.
list=[s*2 for s in range(1,6)]


[2, 4, 6, 8, 10]

Program using lc with filter:

The filter will give only those values which are divisible by 2.

list=[s*2 for s in range(1,6) if s%2==0]


[4, 8]

Dictionary comprehension in Python

The only difference between list and dictionary comprehension is that the dictionary has the keys and values in it. So, the keys and values will come as the expression or value.

Program to use dictionary comprehension:

t="Apoorva"  # string

# dictionary comprehension
dict={x:t[x] for x in range(len(t))}


{0: 'A', 1: 'p', 2: 'o', 3: 'o', 4: 'r', 5: 'v', 6: 'a'}

Program to combine two lists to form a dictionary by using dictionary comprehension method:

# Function definition
def eg1(keys,values):

    # dictionary comprehension
    return{keys[i]:values[i] for i in range (len(keys))}
l1=[1, 'a', 'hello', '%']
l2=['number','character','string','special symbol']

# Function call
print("dc result:",(eg1(l1,l2)))


dc result: {1: 'number', 'a': 'character', 'hello': 'string', '%': 'special symbol'}

Also read:

Leave a Reply

Your email address will not be published. Required fields are marked *