The world’s leading publication for data science, AI, and ML professionals.

List Comprehension in Python

Introduction to List Comprehension

Source
Source

Python provides list comprehension constructs that make it easy to define lists, filter lists, create new lists based on existing lists and much more. Compared to traditional ‘for-loops’, list comprehension is a much more concise and readable alternative to creating lists in python. In this post, we will discuss how to use list comprehension to create lists in python. We will also compare the list comprehension approach to using traditional ‘for-loops’ in each example.

Let’s get started!

First, let’s consider the task of generating a list of positive integers from 1–10 in a ‘for-loop’. Let’s initialize a list called ‘pos_list’:

pos_list = []

We can append values to this list by iterating over the generator returned by the ‘range()’ method in a ‘for-loop’ :

for i in range(1, 11):
    pos_list.append(i)

Let’s print the resulting list:

print(pos_list)

This is simple enough. Now suppose we wanted to generate a list of even integers. We can add a condition that checks if the element is divisible by 2 and append only when the condition is true:

even_list = []
for i in range(1, 11):
    if i%2 == 0:
        even_list.append(i)

Let’s print the result:

print(even_list)

While using a ‘for-loop’ gets the job done, list comprehension provides an easier way to achieve the same results with less code. First, let’s see how to generate our original list of positive integers using list comprehension:

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

Compare this to the original three lines of code we had to use with the ‘for-loop’:

for i in range(1, 11):
    pos_list.append(i)
print(pos_list)

Let’s now use list comprehension to generate the even list:

lc_even_list = [i for i in range(1,11) if i%2]
print(lc_even_list)

Still, this only uses two lines of code compared to the five lines of code using ‘for-loops’:

even_list = []
for i in range(1, 11):
    if i%2 == 0:
        even_list.append(i)
print(even_list)

We can also filter lists quite easily using list comprehension. Let’s consider the task of filtering values less than five from our positive integer list using a ‘for-loop’:

pos_list_gt_5 = []
for i in pos_list:
    if i >= 5:
      pos_list_gt_5.append(i)
print(pos_list_gt_5)

And using list comprehension:

lc_pos_list_gt_5 = [i for i in pos_list if i >= 5]
print(lc_pos_list_gt_5)

As you can see we used much less code. Next, let’s consider the task of iterating over multiple list using list comprehension. Suppose we have our list of integers from 1–10, and another list of integers from 20 -30:

pos_list2 = []
for i in range(20, 30):
    pos_list2.append(i)
print(pos_list)    
print(pos_list2)

Let’s write a ‘for-loop’ iterating over both lists and and append the product of their elements to a new list:

multiply_list = []
for i, j in zip(pos_list, pos_list2):
        multiply_list.append(i*j)

print(multiply_list)

We can perform the same operation using list comprehension:

lc_multiply_list = [i*j for i,j in zip(pos_list, pos_list2)]
print(lc_multiply_list)

We can even apply conditions. Let’s add the product of elements whose product is less than 200:

lc_multiply_list_filter = [i*j for i,j in zip(pos_list, pos_list2) if i*j < 200]
print(lc_multiply_list_filter)

I’ll stop here but I encourage you to play around with the code yourself.

CONCLUSIONS

To summarize, in this post we discuss how to construct lists using list comprehension in python. We showed that while we can complete the same tasks using ‘for-loops’, list comprehension provides a much easier and readable way to construct lists. I hope you found this post useful/interesting. The code in this post is available on GitHub. Thank you for reading!


Related Articles