Python List Comprehension(with examples)

Python List Comprehension

Hello Techies,

This tutorial is based on Python List Comprehension. In this blog, you will learn How to use the Python List Comprehension with the help of multiple examples.

Below are the points we are covering in this blog:

  • Python list comprehension(Introduction)
  • Python list comprehension vs For loop
  • List Comprehension with if-else statement
  • Nested List Comprehensions in Python

Python list comprehension(Introduction)

  • The List comprehension represents the creation of a new list from an iterable object that meets a given condition.
  • List comprehension is usually more compact and faster than the normal for loop for creating lists.
  • If statements can be zero or more.
  • There can be zero or many for the loop.

Syntax of list comprehension:

my_list = [expression for item in iterable_object if_statement]

Examples of list comprehension:

list1 = [i for i in range(10)]
print("List 1 result:", list1)
list2 = [i for i in range(10) if i%2==0]
print("List 2 result:", list2)
list3 = [i for i in range(20) if i%2==0 if i%3==0]
print("List 3 result:", list3)

Output:

List 1 result: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]  
List 2 result: [0, 2, 4, 6, 8]
List 3 result: [0, 6, 12, 18]

Python list comprehension vs For loop

Suppose, if you want to iterate any sequence(List, Dictionary, Tuple, and String, etc…), the first thing on your mind is the for loop.

Let’s check one example, how to use a for loop for iteration.

for i in range(20):
    if i % 2 == 0:
        print(i, "is even Number")

Output:

0 is even Number
2 is even Number
4 is even Number
6 is even Number
8 is even Number
10 is even Number
12 is even Number
14 is even Number
16 is even Number
18 is even Number

However, Python has an easy way to solve this problem using List comprehension in one line code. List comprehension is an easy way to define lists and create lists based on existing listings.

For example,

even_numbers = [i for i in range(20) if i % 2 == 0]
print(even_numbers)

Output:

[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

List Comprehension with if-else statement

We can use list comprehensions for conditional statements to modify an existing list (or other tuples).

Syntax for List Comprehension with if-else statement

my_list = [expression if_statement else expression for intem in iterable_object]

Example of List Comprehension with if-else statement

my_list = [i*2 if i > 5 else i for i in range(10)]
print(my_list)

Output:

[0, 1, 2, 3, 4, 5, 12, 14, 16, 18]

Nested List Comprehensions in Python

The nested list is a list in the list. The nested list comprehension is the same as the nested for the loop.

First, we will check Nested for loop example, where we are writing a program to combine 2 lists in one.

list1 = [[1, 2, 3], [4, 5, 6]]
my_list = []
for i in list1:
    for j in i:
        my_list.append(j)
print(my_list)

Output:

[0, 1, 2, 3, 4, 5, 12, 14, 16, 18]

Let’s develop the above code using the list comprehension.

list1 = [[1, 2, 3], [4, 5, 6]]
my_list = [j for i in list1 for j in i]
print(my_list)

FAQ on Python List Comprehension

Does list comprehension create a new list?

Yes, List comprehension is used to create new lists from other iterations.

Can we use else in list comprehension?

Yes, we can use else in a list comprehension.
See an example below of how to use else condition in the list comprehension,
my_list = [i*2 if i > 5 else i for i in range(10)]


I hope you have understood the concept of list comprehension for more information check the official site.

Leave a Comment