Python: Transpose Matrix

By Xah Lee. Date: .

Here's one way.

# python3
aa = [[1, 2, 3], ['a', 'b', 'c']]
bb = [list(col) for col in zip(*aa)]
print(bb)
# [[1, 'a'], [2, 'b'], [3, 'c']]

Here's another way.

# python3
aa = [ [1, 2, 3], ['a', 'b', 'c'] ]
bb = [[x[i] for x in aa] for i in range(len(aa[0]))]
print(bb)
# [[1, 'a'], [2, 'b'], [3, 'c']]