I want to do a 2X3 transposition in python but can't

Question:

matrix = [['*','*','*'],
          ['*','*','*']]
t_matrix = [['*','*'],
            ['*','*'],
            ['*','*']]
print(list(zip(*matrix)))
[('*', '*'), ('*', '*'), ('*', '*')]

The above is what happens.
I want the matrix to look like t_matrix, but it doesn’t. How can I do the transposition?

Asked By: dada

||

Answers:

Just use a list comprehension to map each tuple to a list.

print([[a, b] for a, b in zip(*matrix)])

More generally:

print([list(x) for x in zip(*matrix)])
Answered By: Chris
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.