Python list comprehension along with examples

In this article, We are going to learn how list comprehension works along with its examples. So basically Python List comprehension is a simple and elegant way of creating a list in python. It returns us a new list after performing specified operation on each element of iterable (List or sequence).

List comprehension requires a input list and operation statement. During the execution of program it takes each element of input list and will perform specified operation on that element. Output after performing operation will be appended to a new list. In short, python list comprehension produces new list after performing an operation on each input list element. Along with input list and operation statement we can optionally specify the conditional statements as well.

In python there are many other way of creating a list. However there are certain benefits of using List comprehension over the other methods.

  • Creation of new list is very easy since the syntax is a single liner.
  • Syntax is easier to read and understand.
  • Possible to test multiple conditions using conditional statements without writing complicated code lines.
what is list comprehension in python? How python list comprehension works?

List comprehension Vs Python for loop Vs Python lambda function:

As said earlier in python there are many ways to create a new list out of existing list. Some of the list creation methods are, “Python for loop” and “Python lambda function“.

To understand advantages of list comprehension over other list creation methods, We’ll first see how we can create a list using “Python for loop” and “Python lambda function” then we’ll see how the same list can be created using “List comprehension“. Here we are going to produce a same list using all the three methods.

Example 1: Suppose we want to create a list of squares of number.

List creation using python for loop:

#Python program to create a list of squares using for loop:
squares_list
= []
for i in range(1, 11):
squares_list.append(i*i)

print(squares_list)

#Output
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

List creation using python Lambda function:

#Python program to create a list of squares using python lambda function:

square_list
= list(map(lambda i: i*i, range(1,11)))
print(squares_list)

#Output
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

List creation using python List comprehension:

#Python program to create squares list using python list comprehension.

square_list = [i*i for i in range(1, 11)]
print(square_list)

#Output
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

Comparing all the ways of creating list which looks more concise and readable.

Syntax of python list comprehension:

List comprehension syntax consists of square bracket containing an expression followed by for loop. Sometimes after “for loop” we can check the conditions as well.

New_List = [Expression for item in list]

Equivalent for loop syntax to the above list comprehension:

New_List = []
for item in list:
     New_List.append(item)

Conditional statements along with list comprehension:

As we know, List comprehension performs operation on each element of input list. Now what if we want perform operation on only specific elements of input list. If we don’t want to perform operation on each element of input list.

So to select only specific elements from input list we can specify conditional statements. These conditional statements will select only those elements which will satisfy the conditional statements. We can write these conditional statements within the list comprehension syntax itself.

Syntax of python list comprehension along with conditional statements:

New_List = [Expression for item in list if condition]

Example 2: Create a list of squares of all the odd numbers between 1 to 10

#Python program to create squares list of odd numbers using python list comprehension.
Odd_numbers_square_list = [i*i for i in range(1, 10) if i%2 == 1]
print(Odd_numbers_square_list)

#Output
[1, 9, 25, 49, 81]

In above example we have specified a condition along with list comprehension i.e ( if i%2 == 1 ) if the element is odd then only append its square value to the new list.

We can check multiple conditions in list comprehension using “and“, “or” keyword.

#Python program to create squares list using python list comprehension.

square_list_with_two_conditions = [i*i for i in range(1, 100) if i%2 == 1 and i*i <= 300]

print(square_list_with_two_conditions)

#Output
[1, 9, 25, 49, 81]

In above example, we have checked multiple conditions using and keyword. Here we want to create a new list of numbers which satisfies these two conditions. The conditions we have specified are, Element on which we are going to perform an operation should odd (i%2 == 1) and also element square value should be less than 300.

During the execution of program we are going to check both the conditions on each input list element. If any element found satisfying both of these conditions then only we are going to append the square value of that element into our new output list.

So that was all about the python list comprehension.

Continue reading:

Leave a Comment