Intermediate

Have you ever found yourself figuring out how to read lists of lists, filtering the list by element attributes, removing elements from a list, or flattening a matrix? Would you like to accomplish all this and write cleaner, comprehensive and much professional code at the same time?
Well once you have completely read this blog You will be not just able to do perform any task regarding lists but become a master handling list’s data and how list comprehension works in python 3.

## List comprehensions

List comprehensions offer a concise way to create lists based on iterable objects such as list, str, and tuples or even some non-sequence types like dict, file objects, any object or method that implements sequence semantics like range() function.

## What is an iterable object?

Iterable objects are known as Iterators and they are behind scenes when loops are used, Iterator in Python is simply an object that returns data, one element at a time.
Imagine an iterator like the structure of a list, so how a list delivers data is through the Iterator, which looks like to the picture below. If you want to go deeper on iterators you can watch the following video

## List comprehension structure

Lists comprehensions consist of an expression which will be iterate followed by a for sentence (this for loop does not require the “:” at the end). It means that a list object can be created in just one line of code without making use of several loops. So the structure will look similar to this Let’s go into a little example where a list of numbers has been given to us and we need to know the square value of each of the given numbers

``````# List of numbers given
numbers = [1,2,3,4,5,6,7,8,9,10]

# We are going to solve this without list comprehension
square_values = []
for number in numbers:
square_values.append(number*number)

# Now we are going to solve with list comprehension
square_vaues_list = [number*number for number in numbers]

# Printing all values
print (numbers)
print (square_vaues_list)
print (square_values)``````
```Output:
# Given numbers
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Without list comprehension solution
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

# List comprehension solution
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]```

As can be seen at the above code there is a list of numbers given from 1 to 10, we needed to know the square value of each element of this list so we iterate the list with a for loop in a way that each number is multiplied by itself on each iteration. A step by step will be like this:

1. 1×1 = 1
2. 2×2 =4
3. 3×3 = 9
4. 10×10 = 100

But this is just the beginning, multiplying a number by itself is not a big deal, so we are going to dig dipper into this and learn how list comprehension works in python 3 in much more detail so we can replace the traditional loops and even lambda functions, once you are able the replace some regular loops with list comprehension you can start using conditional within them to get much cleaner code but first let’s learn how to get conditional within a list conprehension

## List comprehension with conditional statement

In the last example, a for loop was used, but now we are going to be using a different iterable object, now is the turn of range(). As You may know, range() will return a range object which is not a list but an iterable object, meaning that we can iterate each element one by one, keeping in mind that range() is a method it will receive some parameters that are shown on the following table

### IF statement within list comprehension

``` The regular for vs list comprehension statements can be seen at the picture above where a 4 line code is translated into just 1 line and the if statement is added the the formular, if statement is optional but powerful, being added after the input sentence where we declare the scope of the loop.
The if statement filters our list, returning just the values that accomplish the statement```

Now that we know how the structure works and how to replace a regular for loop and filter elements with the if statement, we are going a step forward on the following example.
Let’s imagine that we are looking for factors of 100, being a factor a number that divides 100 without a remainder. e.g: 100/2 = 50
To archive this we will need to use the operator modulo (%) which returns the remainder of dividing two numbers. e.g: 100%2 = 0

``````# We set a range from 1 to 101 so it starts on 1 due to we cannot divede by zero
# Then in the if statement we use the % operator to know if the current number is a factor of 100
factors = [num for num in range(1,101) if 100%num==0]

print (factors)``````
```Output:
# All numbers are 100's factors
# List have been reduce from a range of 100 elements to a list of 9
[1, 2, 4, 5, 10, 20, 25, 50, 100]
```

Until now we have navigated into very nice features that can be useful in your every single day as a program due to iterators are everywhere but let´s know how to handle some headaches that each developer ever have at least once, let{s now learn how to handle a nested for loop, in other words, a for within another loop

## Nested for loops

Nested loops have been the developer´s headache since memorial times but with the following examples, you will master these issues.

First of all, let’s understand what nested for loops are and how it works in a regular way, and how to handle list comprehension. Nested loops are just a loop within a loop meaning that the code will iterate an entire structure for each element of the first object, these kinds of loops could be a matrix or might be different structures look similar to the following image:

But let’s get our hands on it, lets first understand how nested loops work and how can we iterate on them with regular nested for loops

``````# creation of matrix
matrix = []
# iteration from 0 to 3 so 4 vectors are going to be created
for i in range(4):
# append an vector to the matrix
matrix.append([])
for j in range(4):
# append element on vector
matrix[i].append(j)

# print complete matrix
print(matrix)

# print all vectors of the matrix
for vector in matrix:
print (vector)

# print every element of the matrix
for vector in matrix:
for element in vector:
print (element)``````
```output:
#Printing matrix
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

# printing each vector of the matrix
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]

# printing each element of the matrix
1
2
3
4
5
6
7
8
9
```

This seems to be a little bit confusing maybe, but ensure to understand how it works before jumping into list comprehension.
We are going to perform the same matrix creation but now in just one line

``````# creation of matrix
matrix = [[element for element in range(4)] for i in range(4)]

# following the structure of list comprehension, a list comprehension has been added at the outout section getting as result the list created by the second list comprehension

# now same prints than before to compare results

# print complete matrix
print(matrix)

# print all vectors of the matrix
for vector in matrix:
print (vector)

# print every element of the matrix
for vector in matrix:
for element in vector:
print (element)``````
``````t:
#Printing matrix
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

# printing each vector of the matrix
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]

# printing each element of the matrix
1
2
3
4
5
6
7
8
9 ``````

## If you like functional programming

With list comprehension we can replace the use of functional methods like map(), filter(), and reduce()

Here is a video where you can go deeper on this extra topic: https://www.youtube.com/watch?v=JEapLNRuzVo

## Some useful tips

• We should avoid writing very long list comprehensions in one line to ensure that code is user-friendly.
• Remember, every list comprehension can be rewritten in for loop, but every for loop can’t be rewritten in the form of list comprehension.

## Conclusion

• List comprehension is an elegant way to define and create lists based on existing lists.
• List comprehension is generally more compact and faster than normal functions and loops for creating a list.