
Python List Comprehension can be used with Nested For Loops. They can be used, for example, for multi-dimensional matrix operations. Let’s create a 2D matrix first:
matrix = [[col for col in range(4)] for row in range(0,4)]
print(matrix)
#output
#[[0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3]]
Let’s explain what’s going on here:
- The 2D matrix used here is a python list with sublists which give the illusion of row and columns. It uses a nested for loop in the list comprehension.
- [col for col in range(4)] is the inner list comprehension. This creates the columns, which is simply a list of 4 numbers from 0 to 3 inclusive.
- [for row in range(0,4)] is the outer for loop which simply repeats the creation of the column list (the inner list comprehension) 4 times to create 4 rows.
When the list comprehension executes we have a new list matrix which is a 2D matrix.
We can now use another nested for-loop in our list comprehension to transpose this 2D matrix. The transpose of a matrix is a flipped version of the original matrix where the rows and columns are switched.
transposedMatrix = [[row[i] for row in matrix] for i in range(4)]
print(transposedMatrix)
#Output
#[[0, 0, 0, 0], [1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]]
Let’s explain what is going on here:
- [row[i] for row in matrix] is the inner list comprehension for loop which takes the ith element of the ith row and uses it to create the column of the transposed matrix.
- On the first iteration, it takes the 1st element of the 1st row of the original matrix to create the 1st element of the first row of the new transposed matrix. On the second iteration, it takes 1st element of the 2nd row of the original matrix and adds that element to the first row of the new transposed matrix. It continues this until it fills all 4 members of the first row of the new transposed matrix then it moves on to the next row and so on.
- [for i in range(4)] is the outer list comprehension for loop. This simply repeats the process of the inner loop (described above) to create 4 rows. For the first iteration it takes the 1st element of each row of the original matrix to create a new row in the transposed matrix. For the second iteration it takes the 2nd element of each row of the original matrix to create a new row in the transposed matrix, and so on.
- transposedMatrix is the transposed matrix that is returned by our nested-for loop list comprehension.
You can actually do the matrix transposition with a traditional for loop. The problem is that it requires much more code and isn’t as concise:
#tmB = transposedMatrixB
tmB = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]
for r in range(4):
for c in range(4):
tmB[c][r] = matrix[r][c]
print(tmB)
#[[0, 0, 0, 0], [1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]]
As you can see the result is identical but there is more code required. The point of list comprehensions is to do more with less code.
Hope this tutorial helped. Find the full source code HERE and check out another List Comprehension tutorial HERE. Thanks for reading. 👌👌👌