Suppose, we want to separate the letters of the word human and add the letters as items of a list. The first thing that comes in mind would be using for loop.
h_letters = 
for letter in 'human':
When we run the program, the output will be:
['h', 'u', 'm', 'a', 'n']
However, Python has an easier way to solve this issue using List Comprehension. List comprehension is an elegant way to define and create lists based on existing lists.
Let’s see how the above program can be written using list comprehensions.
h_letters = [ letter for letter in 'human' ]
In the above example, a new list is assigned to variable h_letters, and list contains the items of the iterable string 'human'. We call print() function to receive the output.
[expression for item in list]
We can now identify where list comprehensions are used.
If you noticed, human is a string, not a list. This is the power of list comprehension. It can identify when it receives a string or a tuple and work on it like a list.
You can do that using loops. However, not every loop can be rewritten as list comprehension. But as you learn and get comfortable with list comprehensions, you will find yourself replacing more and more loops with this elegant syntax.
List comprehensions aren’t the only way to work on lists. Various built-in functions and lambda functions can create and modify lists in less lines of code.
letters = list(map(lambda x: x, 'human'))
When we run the program, the output will be
However, list comprehensions are usually more human readable than lambda functions. It is easier to understand what the programmer was trying to accomplish when list comprehensions are used.
List comprehensions can utilize conditional statement to modify existing list (or other tuples). We will create list that uses mathematical operators, integers, and range().
number_list = [ x for x in range(20) if x % 2 == 0]
When we run the above program, the output will be:
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
The list ,number_list, will be populated by the items in range from 0-19 if the item's value is divisible by 2.
num_list = [y for y in range(100) if y % 2 == 0 if y % 5 == 0]
[0, 10, 20, 30, 40, 50, 60, 70, 80, 90]
Here, list comprehension checks:
If y satisfies both conditions, y is appended to num_list.
obj = ["Even" if i%2==0 else "Odd" for i in range(10)]
['Even', 'Odd', 'Even', 'Odd', 'Even', 'Odd', 'Even', 'Odd', 'Even', 'Odd']
Here, list comprehension will check the 10 numbers from 0 to 9. If i is divisible by 2, then Even is appended to the obj list. If not, Odd is appended.
Suppose, we need to compute transpose of a matrix which requires nested for loop. Let’s see how it is done using normal for loop first.
transposed = 
matrix = [[1, 2, 3, 4], [4, 5, 6, 8]]
for i in range(len(matrix)):
transposed_row = 
for row in matrix:
#Output: [[1, 4], [2, 5], [3, 6]]
The above code use two for loops to find transpose of the matrix.
We can also perform nested iteration inside a list comprehension. In this section, we will find transpose of a matrix using nested loop inside list comprehension.
matrix = [[1, 2], [3,4], [5,6], [7,8]]
transpose = [[row[i] for row in matrix] for i in range(2)]
[[1, 3, 5, 7], [2, 4, 6, 8]]
In above program, we have a variable matrix which have 4 rows and 2 columns.We need to find transpose of the matrix. For that, we used list comprehension.
**Note: The nested loops in list comprehension don’t work like normal nested loops. In the above program, for i in range(2) is executed before row[i] for row in matrix. Hence at first, a value is assigned to i then item directed by row[i] is appended in the transpose variable.
for i in range(2)
row[i] for row in matrix