Python list comprehension: The Complete Guide

Lists are one of the used data types in Python, which allow users to enter heterogeneous elements. They can add elements of different data types. Let it be int, char, string, float, or any other data type.

Python list comprehension

List comprehension offers a shorter syntax to create lists based on existing iterables in Python. As a result, list comprehensions are relatively faster than for loops. It is helpful when you want to create a new list based on the values of an existing list.

Syntax

listComprehension = [expression for item in iterable if condition == True]

Example

cars = ["bmw", "audi", "mercedez", "jaguar", "land rover"]

newlist = [x for x in cars]

print(newlist)

Output

['bmw', 'audi', 'mercedez', 'jaguar', 'land rover']

You can see that we created a list from an existing list using list comprehension.

Python list comprehension with if condition

You can write an if condition while using the list comprehension. Then, it will return a list based on the condition you provide.

cars = ["bmw", "audi", "mercedez", "jaguar", "land rover"]

newlist = [x for x in cars if "e" in x]

print(newlist)

Output

['mercedez', 'land rover']

We create a new list where only the elements contain the letter “e” in this example.

Python list comprehension with if-else

The if-else is a decision-making statement that executes a code only if satisfied a specific condition.

dipam = ["chiku", "chikyaaa", "chikudi", "dimpui", "Sapodilla"]
newlist = ["dipam" if i%2==0 else "sushi" for i in range(5)]
print(newlist)

Output

['dipam', 'sushi', 'dipam', 'sushi', 'dipam']

Here we can see that the output list contains “dipam” when the condition is TRUE, and when the condition is FALSE, the output is “sushi“.

Python list comprehension with for loop

A for loop in Python is used for iterating over a sequence. Therefore, we can comprehend the list using for loop.

dipam = ["chiku", "chikyaaa", "chikudi", "dimpui", " Sapodilla"]
newlist = []

for fries in dipam:
   if ("c" in fries):
     newlist.append(fries)
print(newlist)

Output

['chiku', 'chikyaaa', 'chikudi']

And if we use LIST COMPREHENSION, the code is short, simple, and easy to understand.

dipam = ["chiku", "chikyaaa", "chikudi", "dimpui", " Sapodilla"]
newlist = [fries for fries in dipam if "c" in fries]
print(newlist)

Output

['chiku', 'chikyaaa', 'chikudi']

Python list comprehension with two lists

We can comprehend two lists in a list comprehension. List comprehensions are equivalent to for-loops.

d1 = ['chiku', 'chikyaaa', 'chikudi']
d2 = ['Dimpui', 'Sapodilla']
list_3 = [x + y for x in d1 for y in d2]
print(list_3)

Output

['chikuDimpui', 'chikuSapodilla', 'chikyaaaDimpui', 
'chikyaaaSapodilla', 'chikudiDimpui', 'chikudiSapodilla']

We comprehended two lists using list comprehension and then combined them using the “+” operator.

Summary

In this article, we learned about List Comprehension using multiple if statements, if-else statements, for loop and list comprehension using two lists. List Comprehension makes the code simple, sweet, short, and easy to understand.

That’s it for this tutorial.

Leave a Comment