Nested list comprehension
matrix = [
... [1, 2, 3, 4],
... [5, 6, 7, 8],
... [9, 10, 11, 12],
... ]
The following list comprehension will transpose rows and columns:
[[row[i] for row in matrix] for i in range(4)]
#output
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8...
snowcodes.hashnode.dev2 min read