A brief introduction.
This is a brief introduction on how to write list comprehensions. List comprehensions can be very helpful and sometimes more flexible than a regular For loop.
To begin, let’s start with a simple For loop. We’ll start with a list of numbers from 1 to 10 and iterate them through a For loop to be multiplied by 2 and assign it to a new list.
nums = [1,2,3,4,5,6,7,8,9,10]
nums2 = for i in nums:
nums2.append(i * 2)print(nums2)
As we saw above, this took quite a bit of space and may take more processing time since we first are running each item in the loop, then multiplying it by 2, and finally adding it to the new list. But what if we want to iterate it through the same list? we would have to add extra code to empty the list and then add the new numbers.
Let’s try it with a list comprehension. A list comprehension you can use to create powerful functionality within a single line of code allowing you to write code that’s elegant, easy to write, and almost as easy to read like plain English.
We’ll start by telling it what to do with the item that we want to iterate and then add the For loop statement.
nums = [1,2,3,4,5,6,7,8,9,10]nums2 = [i * 2 for i in nums]print(nums2)
That was super simple, took just one line to write! A simple way to interpret it or to help you write a list comprehension is to think about it in simple English. For example, I would read this as “Multiply the item by 2 for each item in nums” and that’s exactly what is happening. Simple right?
Now we can also use list comprehension to iterate through the list itself! Here’s an example:
nums = [1,2,3,4,5,6,7,8,9,10]nums = [i * 2 for i in nums]print(nums)
Simple! This list comprehension permanently modified the original list, but if that’s our goal, we didn’t need several lines of code to be able to do so. It just took a list comprehension.
List comprehension can also be used with functions just like any regular list. Let’s say we want to only get the sum of all the numbers that were multiplied by two or see the minimum of those numbers, there’s a way! Here’s the example:
nums = sum([i * 2 for i in nums])
110nums = min([i * 2 for i in nums])
2nums = max([i * 2 for i in nums])
List comprehensions can be more complex than just a simple For loop. We are also able to add If statements to it, let’s try it out! To do so, you’ll need to place the If statement before the For loop, the If statement affects each item that the For loop is returning. When placed before the For loop keep in mind that the If statement must have an Else statement unlike an If statement outside a list comprehension.
In the following example, we want to iterate through the list and see if the item is even or odd. If it’s even, leave it as it is, if not, then return a 0. This is what it looks like:
nums = [1,2,3,4,5,6,7,8,9,10]nums= [i if i%2 == 0 else 0 for i in nums]print(nums)
[0, 2, 0, 4, 0, 6, 0, 8, 0, 10]
We can read it as “Give me the item if the item’s floor division equals 0, else, give me 0 for each item in the list of nums” This has its practical uses but in this case, it doesn’t look too pretty. Since we don’t really care for the 0 in the list, we can use a list comprehension to only give us the numbers that are even.
Here’s when the second position of the If statement comes in handy. If we place the If statement after the For loop, we affect what the output of the For loop is. When used after the For loop, we don’t need to provide an Else statement. Here’s an example where we only want the list comprehension to return the even numbers:
nums = [1,2,3,4,5,6,7,8,9,10]nums= [i for i in nums if i%2 == 0]print(nums)
[2, 4, 6, 8, 10]
We can read this as “Give me each item in the list of nums if the item’s floor division equals 0”.
There we go! a simple introduction to List comprehensions. In the next blog we’ll look at nested If statements and nested For loops and we’ll dive deeper on how to use functions within list comprehensions.