What is List Comprehension in Python?

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!
Video Tutorial
List Comprehension

A feature of Python that many programmers love is its ability to write code that is elegant, easy to read, and almost as easy to write as plain English. Python's list comprehension feature allows you to create powerful functionality with just a few lines of code.

List comprehension in Python allows us to create a list in Python using a very shorter and simpler syntax. Basically, it is a simpler way to create a new list from the values in a list we already have. It is a very popular syntax used by many programmers to make their code look very elegant or one-liner. List comprehenstion It is generally a single line of code which are enclosed in square brackets.

Just like in the above image, we have 3 phases of codes in a single line, enclosed in brackets. The first part dictates the output, the second part - the iterable, and the third part - the condition.

Takeaway: List comprehension in Python is a single line of code that we write inside the square brackets. It is popularly known as the one-liner code. And, List comprehension offers a shorter and simpler syntax for constructing new lists based on values in existing lists.

Syntax of List Comprehension in Python

Below given is the syntax of the list comprehension in Python:

List comprehension mainly has three components:

  1. For loop: It is mandatory and loops over the given iterable.
  2. Condition and expression: It is optional and is used to imply some condition over the iterable
  3. Output: It is mandatory and the variable(or expression) we provide as output is added to our list.

Syntax of List Comprehension

The loop iterates over the iterable to create the new list. The condition keeps a check on whether to include or not the list item(or any other check) based on some condition and finally, we also have our output or expression evaluating which each new element is added to our list.

We will better understand this through code. Let us look into how to create list comprehension in Python.

Example of Simple List Comprehensions in Python

Let us look into the very basic usage of list comprehension in Python, and then we will move to more useful and practical examples ahead.

Example 1: Simple Loop

Code:

Output:

Explanation:

  • In the above example, we have a for loop which iterates over the passed list. However, we do not have any conditions here.
  • For output, we have passed i. So, our output is simply the elements of the iterable in our new list.

Adding Some Conditions to Above Code -- Let us now add some conditions to our previous code and check how it works.

Code:

Output:

Explanation: Did you just see, that we added a condition that checks if the elements of our list are even or odd? So this time, our list filters only the even numbers from the list passed as parameters and adds to our new list.

Must Note: Also, you must note that using list comprehension we are creating a completely new list based on our conditions, iterables, etc. However, we DO NOT MODIFY the original list passed to us as the "iterable". Twisting our output in the resultant list -- Let us now add some twists in the value of output which we will get after the list comprehension:

Code:

Output:

Explanation: We just squared the value of the elements which we are getting after applying the condition on our passed iterable. So, this is how we can use any expression in our output(here i for example) to modify the elements of our list.

Using our Regular For Loops

For better understanding, you can also refer to the same below code, which is written without using list comprehensions (using only for loop):

Code:

So, you must have seen, how this multi-line code was written in a single line using the list comprehension.

Example 2: List Comprehension With a Nested For Loop

Now, we will learn how can we use list comprehension using nested for loops in Python.

Code:

Output:

Explanation: Here we are given a list of list, out of which we are creating another list of list and a simple list, using the list comprehension. Please note the manner we have written our code:

  1. [[i for i in j] for j in ls] this statement first runs this piece of code: for j in ls, it picks lists from ls into j.
  2. After that, [i for i in j] this line picks up each element from the list into i and adds it to another list.

The above code was for generating list of list. However, if we want to generate a single list, we follow the below steps:

  1. [j for i in ls for j in i] In this line, we first run our loop through the list ls, and put the result into i.
  2. After that, we loop through i using the j loop. And, we output our j as result.

Exception: Suppose, for generating a plain list, we would have written this code:

Code:

Output:

It threw an error because, our Python compiler will read the code from left to right, and the moment it finds for i in j, it will straightaway throw an error that j is not defined because we have not yet put any element of ls into j.

Equivalent code:

This is the equivalent for loop code for the above list comprehension. You can see how efficiently we have coded this so many lines of code into one-liners in the above examples. This is where list comprehensions are very useful, and look elegant as well.

Using Conditional Logic in List Comprehensions

We have already seen how can we use conditional logic in list comprehensions. But now, we will discuss how can we use nested conditions in List Comprehensions.

Example 1: List Comprehensions with Multiple If and Else Conditions

Code:

Output:

Explanation:

  • Firstly, our loop runs for a in ls and then it matches with the condition a+10 if a<10 else a+5. Based on the condition, it fills the value into the list.
  • To visualize it better, let us see the equivalent for loop code below.

Equivalent code:

Above given is the example for the equivalent for-loop code for the list comprehension.

Example 2: List Comprehensions with if, elif and else Conditions:

Code:

Output:

Explanation: This is a nested condition that has 3 parts. Based on the condition, it renders every part. a*2 if a<10 checks for a < 10, otherwise, else a**2 if not a%2 this code will run, but if both the above conditions are false, then else a+5 will run. Let us see the equivalent code for better understanding:

Equivalent code:

After reading the above code, you will easily be able to visualize the code we have written for the list comprehensions.

Using the Walrus Operator in List Comprehensions

Python 3.8 introduced the walrus operator, which allowed us to accomplish two tasks at the same time:

  1. assigning a value to a variable
  2. returning the value at the same time

It sometimes leads to shorter, more readable code, which is also more computationally efficient.

Syntax: Walrus operator is popularly known by the symbol ":="":=". Below given is the syntax for the walrus operator:

Here, expression is evaluated and then assigned to the variable name. That value will also be returned.

Takeaway: Walrus operator allows us to both assign a value to a variable, and to return that value, all in the same expression.

Code:

Output:

Explanation: Here, the walrus function is defined to return the records year whichever is greater than 2010. The operator := takes a record year generated by the function record_year through this line: rec := record_year()) >= 2010. And, at the same time, it assigns the record year to the variable rec. The for loop runs 10 times, where every time the if the condition meets, a value is assigned to rec.

It's not often necessary to use walrus assignment expressions inside a list comprehension in Python, but it's a good tool to have when needed.

When to Use a List Comprehension in Python?

Before discussing the use cases or when to use the list comprehension in Python, let us see some benefits of list comprehension in Python.

Benefits of list comprehension: List comprehension is faster, simpler and reliable in many ways. But, it is usually faster if we want to create a new list out of a list in Python. List comprehension is also considered more pythonic than using for loops or other ways to construct a list in Python. One of the biggest advantages of list comprehension is that they allow developers to write less code that is often easier to understand(if written in an understandable way).

Now to answer the question when to use list comprehension in Python, we have the following cases:

  • To filter an existing list.
  • To generate a list of data
  • Flattening a multidimensional list

So, we can use list comprehension if we are doing simple filtering, modifications, or a formatting task on some iterative objects. Or, if we are flattening a multidimensional list. Apart from that, we don't need to append values while creating a list. Also, you can use it when even a tiny bit of performance matters to you. So, there are several advantages of list comprehension in Python.

When not to use list comprehension in Python?

There are many situations where list comprehensions can be useful and can help us write clean, easy-to-read, and debuggable code, but they aren't the best choice in every situation.

In some cases, we may simply need to print the results of an iteration of a list. In that case, list comprehension is not an obvious advantage. In fact, at that point, we create an unnecessary list, using unnecessary memory.

We should avoid using list comprehension if we have too many conditions to add for filtering or modifying as it will make our code more complex and harder to read.

Again, it's probably better to pick an alternative if list comprehension makes your code run slower or uses more memory. If our code is less performant or harder to understand, then it might be better if we avoid them.

Takeaway: A simple question we can use to decide whether or not list comprehension would be a better alternative to a loop is, "whether I'm creating a new list" If the answer is "yes", then list comprehension should be considered. Otherwise, using the regular loop is the best option.

Examples

Example 1: Iterating Through a String Using List Comprehension

Code:

Output:

Explanation:

In the above code, we have iterated through a string using list comprehension in Python.

  • In the first list, [a for a in str], we do a simple iteration through the string and using list comprehension we create a list.
  • In the second list, we filter the list using a condition, [a for a in str if a.lower() in 'aeiou'] which checks whether any element of list is a vowel. Based on that, we create our second list.
  • In the third list, we modify our output by appending some character to it like ["|"+a+"|" for a in str].

Example 2: Transpose of a Matrix using List Comprehension

Transpose of a matrix is the interchanging of rows and columns. It is denoted as AA'. The element at ithi^{\text{th}} row and jthj^{\text{th}} column in AA will be placed at jthj^{\text{th}} row and ithi^{\text{th}} column in XX'. So if AA is a 3x2 matrix, AA' will be a 2x3 matrix.

Code:

Output:

Explanation: In the above example, we have computed the transpose of the matrix using the List Comprehension in Python. For that, we use two loops in Python and apply our transpose matrix logic. To understand it better, below given is the equivalent code for transpose of a matrix in Python.

Code:

List Comprehensions vs Lambda functions

Lambda functions: Lambda functions are anonymous functions in Python. Anonymous functions are those functions that do not have any name. The normal functions are defined using the def keyword, whereas the lambda functions are defined using lambda keyword.

Syntax:

Lambda cannot be used by itself to iterate through a list. Therefore, we have map here, which takes a function and an iterable, and we can iterate through it. So we can use a lambda function inside of our map. Map returns a list. But to actually convert our whole output into a list, we wrap it in the List() function.

Map takes in two arguments: a function and an iterable. This allows us to use a lambda function we’ve created, or any function in that matter, to iterate through whatever list or array we desire. In return, Map will give us a list that we can use and manipulate for whatever purpose we want later on.

What is the difference? The difference between Lambdas and List Comprehension is that List Comprehension is used to create lists, whereas, lambdas are functions that act exactly like other functions and hence return any values or list.

Let us see some examples for list comprehension and lambda functions.

Example:

Let us see the code for the usage of a simple for loop using list comprehension and lambda expression. Code:

Output:

Explanation: As explained, lambda uses map to iterate over the lambda expression and finally wrap the complete result into the list.

Which one to use - List Comprehension / Lambda ?

The performance difference between the two approaches is just marginal. Hence, whether to use map + lambda or list comprehensions comes down to personal opinion. However, some people prefer List Comprehensions over lambda and map, as they say using "list comprehension" is a more pythonic and systematic way of coding in Python.

This article included all the major points you need to know to get a brief idea about List comprehensions in Python. You may also refer to the below scaler articles to know more about Lists and Comprehension in Python --

Conclusion

In this article, we learned how to use Python's list comprehension to solve complex tasks without making our code too complicated. Let us summarize what we learned throughout:

  • List comprehension is a single line of code written inside the square brackets. It makes our code shorter and look more pythonic.
  • We can use list comprehension in Python whenever we want to create a new list or have simple filtering tasks.
  • We should avoid using list comprehension if we have too many conditions to add for filtering or modifying, as it will make our code more complex and harder to read.
  • There is no such performance difference between list comprehension and lambda function, hence it is a personal choice for us to use any of these.