List comprehension in Python

One of the many reasons I like to code in Python is the amount of code that we have to write. There are so many inbuilt functionality that makes coding in Python very easy and fun.

Let’s look at one such feature in Python which is List comprehension.

List comprehension allows us to reduce the number of lines that we have to write when picking out a subset of a list based on a condition. So what do I mean by subset of a list? Let’s look at an example to understand it better way.

Let’s say you have given a list of numbers and you have to make another list that contains only even numbers. Easy, right? Yes. So, Let’s write some code in Python for this example.

For example to list out even numbers without list comprehension.

Let’s understand what we did here. So we have a list of numbers, and then we declare another list for even numbers which is empty at the start. Last we have a loop in which we have an if statement where we are checking modulus of 2 is 0, if true we are adding numbers to the list. Easy…

Now let’s take a look at the same example with list comprehension.

For example to list out even numbers with list comprehension.

Okay, so in the above example, we have a line where we are doing something, but the question is what? So the answer is, exactly the same as what we did in the previous example but with one line.

So before diving deep into it, Let’s list out some important things that we need for list comprehension.

  1. loop for iteration of numbers in list (old_list)

  2. Condition to check (condition)

  3. the value that is being added to a new list (expression)

Syntax:

[expression for an item in old_list if condition]

In short, only those items that satisfy the condition will be passed to the expression and the result of the expression will be added to the list. Expression can be anything a function or just a variable. In the above example, we want to add an item that satisfies the condition, so we used the item (which is i in our case)in place of expression.

To get a clear idea of the expression part, let's look at a few more examples.

  1. List out squares of even numbers in the given list

code to list out squares of even numbers in the given list

In the above example, You can see we are actually multiplying the number (at the place of expression in syntax) by itself to get the square of an even number.

2. List out squares of all numbers in the given list

Code to list out squares of all numbers in the given list

In this example, we have actually omitted the condition part and passed an item to the function. As a result, we are getting a square of numbers in the new list. So, It is not necessary to have a condition. In function, we can write any logic and return a value which will then get added to the new list.

Hopefully, You have understood the concept of list comprehension. I have tried to explain it as well as possible. If you still have any questions, You can ask me in a comment below or follow me on Twitter and drop a message. I’ll try to clear your doubts.

Thanks!