List Comprehension in Python

Previously in this Python tutorial, we have already learned what Python lists are and how to use them. We have also understood list comprehension in Python. In this section, we will go deep into this topic and understand why we need it.
Following is the list of all topics that we will cover in this section:

Certification in Full Stack Web Development

What is Python List Comprehension?

Some of the programming languages have a syntactic construct called list comprehension for creating lists on the basis of existing lists. Python is one such language. In other words, list comprehensions are used for converting one list into another list or creating a new list from other iterables.
A list comprehension consists of:

  • Input sequence
  • A variable to store members of the input sequence
  • Predicate expression
  • Output expression that produces the output list based on the input sequence and also satisfies the predicate

Let’s take a brief look over Python lists before starting with Python list comprehensions.

Become an expert Python Developer, Enroll in our Python course in Bangalore.

Python Lists

Python list is a compound data type in Python where we can group together various values. These values need not be of the same type; we can combine Boolean, string, and integer values together and save them as lists.
The syntax of Python lists consists of two square brackets inside of which we define our values separated by commas. These values can be of any data type.

Python List Comprehension Example:

list1 = [1,2,3,4,5]
list2=[“hello”,”intellipaat”]

Syntax

The syntax of List Comprehension Python consists of square brackets inside of which we write expressions followed by a “for clause” and then by “for or if clauses” as needed.
The expressions can be anything. We can input any kind of object in the lists.

[ expression for an item in list if conditional ]

When compared to a normal Python Lists syntax, the above syntax is equivalent to

for an item in the list:
if conditional:
expression

The resultant is a new list that is created after the evaluation of the expression in accordance with the ‘for’ and ‘if’ clauses provided after the expression.
Thus we can replace the following code for defining and creating a list in Python:

new_list = []
for i in old_list:
if filter(i):
new_list.append(expressions(i))

Following is the equivalent code in List Comprehension Python to obtain the same result:

new_list = [expression(i) for i in old_list if filter(i)]

Here,

  • new_list: The name of the resultant new list
  • expression(i): “i” here is the variable name and expression is based on this variable which is used for every element in the old list
  • for i in old_list: “for” iteration using the variable in the old list
  • if filter(i): filter applied with an if statement

Know How to Sort a List in Python Without Using Sort Function from our comprehensive blog!

Get 100% Hike!

Master Most in Demand Skills Now !

Why Should We Use List Comprehension in Python?

List comprehension is an elegant and concise way of creating a list in Python. There are many benefits of using Python List and the most basic benefit can be seen from the syntax alone. We reduced three lines of code into a one-liner. Not just that but the code in Python List comprehension is much faster also. With Python List comprehension, instead of having to resize the list on runtime, python will now allocate the list’s memory first, which makes it faster in this case.
Moreover, the code using Python List comprehension is considered to be more fitting in the guidelines of Python making it more Pythonic in nature.

Examples:

Now that we know the syntax of Python List comprehensions, let us try out some examples.
Example 1: Let’s start by creating a simple list.

x = [i for i in range(15)]
print (x)

Output:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

 

Example 2: Now let’s create a list using the “for” clause in a list comprehension.

cubes = [x**3 for x in range(5)]
print (cubes)

Output:

[0, 1, 8, 27, 64]

 

Example 3: Let’s create a new list by making some changes in an already existing list.

list1 = [3,4,5]
new_multiplied_list = [item*2 for item in list1]
print (new_multiplied_list)

In the above code block, the new list is named new_multiplied_list, and it is obtained by multiplying every element of the old list, which is named as list1, with 2.
Output:

[6, 8, 10]

 

Example 4: Now let’s create a new list containing the first letters of every element in an already existing list.

listOfWords = ["this","is","python","tutorial","from","intellipaat"]
new_list = [ word[0] for word in listOfWords ]
print (new_list)

Output:

['t', 'i', 'p', 't', 'f', 'i']
Go through these Python Technical Interview Questions to crack your interviews.

List Comprehension Python vs For Loop in Python

Whenever we want to repeat a block of code a fixed number of times, the first way of doing it is using “for loops.” List comprehensions are also capable of doing the same and that too with a better approach than for loops since list comprehensions are more compact. Hence, it provides a very good alternative for loops.
Let’s take an example here. Say, we want to separate the letters of a word and create a list containing those letters.

The code block for the same in case of using for loop will be:

letters = []
for letter in 'Intellipaat':
letters.append(letter)
print(letters)

Output:

['I', 'n', 't', 'e', 'l', 'l', 'i', 'p', 'a', 'a', 't']

 

We can obtain the same result using List Comprehension with a lesser number of code lines as shown below:

letters = [ letter for letter in 'Intellipaat' ]
print(letters)

Output:

['I', 'n', 't', 'e', 'l', 'l', 'i', 'p', 'a', 'a', 't']

While using Python list comprehensions, we don’t necessarily need a list to create another list. We can use strings as well, and list comprehension will identify it as a string and work on it as a list. In the above example of list comprehension code block, Intellipaat is a string, not a list.

Become a Full Stack Web Developer

List Comprehension Python Vs. Python Lambda Functions

List comprehensions and lambda functions are two powerful tools in Python that can be used to create and manipulate lists. They both have their own advantages and disadvantages, so the best choice for a particular task will depend on the specific requirements.

List comprehensions are a concise way to create new lists from existing lists. They are often faster than using other methods, such as map() or filter(). However, list comprehensions can be difficult to read and understand, especially for complex expressions.

Here is an example of a list comprehension:

Python

numbers = [1, 2, 3, 4, 5]

squared_numbers = [number ** 2 for number in numbers]

print(squared_numbers)

Use code with caution. Learn more
This code creates a new list called squared_numbers that contains the squares of the numbers in the original list numbers. The list comprehension is concise and easy to read, but it can be difficult to understand if the expression inside the square brackets is complex.

Lambda functions are anonymous functions that can be used to perform simple calculations. They are often used in conjunction with list comprehensions to create more complex expressions. Lambda functions are easier to read and understand than list comprehensions, but they can be slower.

Here is an example of a lambda function:

Python

square_function = lambda number: number ** 2

squared_numbers = [square_function(number) for number in numbers]

print(squared_numbers)

Use code with caution. Learn more
This code creates a lambda function called square_function that takes a number as input and returns its square. The lambda function is easy to read and understand, but it is slower than the list comprehension in the previous example.

In general, list comprehensions are a good choice for simple tasks, while lambda functions are a good choice for complex tasks. However, the best choice will always depend on the specific requirements of the task.”

Map() with Lambda Function

Let’s first see how we use map() with Lambda function to work on lists:

letters = list(map(lambda x: x, 'intellipaat'))
print(letters)

In the above block of code, we have used map() with lambda to create a list containing the letters of the string “intellipaat” separated by commas. The name of the list is the letters.
Output:

['i', 'n', 't', 'e', 'l', 'l', 'i', 'p', 'a', 'a', 't']

We can obtain the same result using Python List Comprehensions. The codes in comprehension are also more human-readable and easier to understand.
Follow the following steps to write the equivalent Python List comprehension code:

  • After naming the new list, start with the square brackets
  • Include the variable name that we will use to iterate throughout the elements of the old list or, in our case, the string
  • Add the for clause to repeat the sequence element, that is, our variable
  • Specify where the variable comes from. Add the in keyword followed by the sequence from where we will get our variable. In our case, we will use the Intellipaat string to transform the elements of our new list

So the final code using Python List Comprehension looks like this:

New_list=[ x for x in 'intellipaat']
print(new_list)

Output:

['i', 'n', 't', 'e', 'l', 'l', 'i', 'p', 'a', 'a', 't']

Wish to gain an in-depth knowledge of python? Check out our Python Tutorial and gather more insights!

Career Transition

Non-Tech to IT Associate | Career Transformation | AWS Certification Course - Intellipaat Reviews
Non Tech to DevOps Engineer Career Transition | Intellipaat Devops Training Reviews - Nitin
Upskilled & Got Job as Analyst After a Career Break |  Data Science Course Story - Shehzin Mulla
Successful Career Change after Completion of AWS Course - Krishnamohan | Intellipaat Review
Got Job Promotion After Completing Artificial Intelligence Course - Intellipaat Review | Gaurav
Intellipaat Reviews | Big Data Analytics Course | Career Transformation to Big Data | Gayathri

Filter() with Lambda Function

Now that we know how we can use Python list comprehension as an alternative for the map() function combined with lambda, let’s now see how we can use Python list comprehensions as an alternative for the filter() function.

We have used filter() with lambda to filter out odd values from an existing list and save the filtered values in a new list in the following code block.

list1 = [1,2,3,4,5,6,7,8,9,10]
list1 = list(map(int, list1))
new_list= filter(lambda x: x%2, list1)
print(list(new_list))

Output:

[1, 3, 5, 7, 9]

The same result can be obtained using Python List Comprehension as shown below:

list1 = [1,2,3,4,5,6,7,8,9,10]
print(list1)
new_list = [ x for x in list1 if x%2 ]
print(new_list)

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 3, 5, 7, 9]

Reduce() with Lambda Function

As mentioned above, we can also write the lambda function codes with the reduce() function to make the code short and more efficient using Python list comprehensions.
The following block of code is an example of reduce() with lambda:

from functools import reduce
list1 = [1,2,3,4,5,6]
new_list = reduce(lambda x,y: x+y, list1)
print(new_list)

Note: Recently, the reduce module was moved to the functools package, so If we are using Python 3, then we will have to import the reduce module from functools as shown in the above code block.
The resultant list contains the sum of all the elements of the list, list1.
Output:
21
The same result can be obtained using Python list comprehensions as shown below:
Note: Python list comprehensions only work with one variable, so the use of Y here is not allowed. Hence, to perform the above task, we can use an aggregation function, such as sum().

list1 = [1,2,3,4,5,6]
new_list = sum([x for x in list1])
print(new_list)

Output:
21
Note: Here, we did not have to import the reduce module, because we replaced it with Python list comprehension.

Learn new Technologies

Conditionals in Python List Comprehension

We can also make use of conditional statements in list comprehensions to modify and manipulate lists. Let’s learn how to do that with the help of some examples. Here, we will use a mathematical function range() that defines the range that we want to use in our examples. It takes one integer as a parameter and the range starts from 0 to the number one less than the parameter provided. For example, range(20) will consider the range of numbers from 0 to

Examples:

1. Using if statement in Python List comprehension

new_list = [x for x range(20) if x%2==0]
print(new_list)

Output:

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

 

2. Using Nested IF with Python List Comprehension

new_list = [x for x in range(50) if x %2==0 if x%5==0]

Output:

[0, 10, 20, 30, 40, 50]

In the above example, the first condition that the list comprehension checks is if x is divisible by 2, and then if the condition is met it encounters the other conditional statement and checks if x which was found to be divisible by 2 is also divisible by 5.
If both the conditions are met, only then x is appended to the list new_list.

3. Using if-else statement with Python List comprehension

even_odd = [f"{x} is even" if x%2==0 else f"{x} is odd" for x in range (10)]
print(even_odd)

Output:

['0 is even', '1 is odd', '2 is even', '3 is odd', '4 is even', '5 is odd', '6 is even', '7 is odd', '8 is even', '9 is odd']

In the above example, the list comprehension checks all numbers starting from 0 to 9. If x is found to be divisible by 2, ‘x is even’ (where x is the respective number) is appended to the even_odd list. If the condition is not met, then the else statement is executed, and ‘x is odd’( where x is the respective number) is appended in the even_odd list.

Nested Lists in Python List Comprehension

Whenever we talk about nested lists, the first method to implement nested lists that comes to our mind is using nested loops. As we have already seen that Python list comprehension can be used as an alternative for loops, obviously it can also be used for nested loops. Let’s see how to do it with the help of an example.
Let’s say, we want to display the multiplication tables of 4, 5, and 6. Now using regular nested for loops, we would write the following code block to implement the same:

for x in range(4,7):
for y in range(1,11):
print(f"{x}*{y}={x*y}")

The output would be:

4*1=4
4*2=8
4*3=12
4*4=16
4*5=20
4*6=24
4*7=28
4*8=32
4*9=36
4*10=40
5*1=5
5*2=10
5*3=15
5*4=20
5*5=25
5*6=30
5*7=35
5*8=40
5*9=45
5*10=50
6*1=6
6*2=12
6*3=18
6*4=24
6*5=30
6*6=36
6*7=42
6*8=48
6*9=54
6*10=60

We can obtain the same result using Python list comprehension, and the code block used will be:

table = [[x*y for y in range(1,11)] for x in range(4,7)]
print(table)

Output:

[[4, 8, 12, 16, 20, 24, 28, 32, 36, 40], [5, 10, 15, 20, 25, 30, 35, 40, 45, 50], [6, 12, 18, 24, 30, 36, 42, 48, 54, 60]]

In the above example, we have used ‘for loop’ for y as the inner comprehension.

Key Takeaways

  • Python list comprehension is a very concise and elegant way of defining and creating lists based on existing lists.
  • When it comes to creating lists, Python list comprehensions are more compact and faster than loops and other functions used with lambda, such as map(), filter(), and reduce().
  • Every Python list comprehension can be rewritten for loops, but not every complex for loop can be rewritten in Python list comprehension.
  • Python list comprehensions are considered to be more Pythonic because of how compact they can be and how readable they are.
  • Writing very long list comprehension in one line should be avoided so as to keep the code user-friendly.

Conclusion

Python list comprehensions are found to be faster and more user-friendly than for loops and lambda functions. The speed and readability are what makes Python list comprehensions a favorable alternative for loops and lambda function. Mastering this comprehension can make our codes more efficient, faster, and shorter. While we have covered all main topics, along with examples, there is more to learn about Python.

Want to gain in-depth knowledge and up-to-date study material? Check out this well-structured Python Certification Training Course by Intellipaat. This course will help me master all main modules and packages in Python, web scraping libraries, lambda function, and more. Through this course, learn how to write Python codes for Big Data systems, such as Hadoop, and get plenty of hands-on exercises to practice and master Python.

Course Schedule

Name Date Details
Python Course 30 Mar 2024(Sat-Sun) Weekend Batch
View Details
Python Course 06 Apr 2024(Sat-Sun) Weekend Batch
View Details
Python Course 13 Apr 2024(Sat-Sun) Weekend Batch
View Details