12

Possible Duplicate:
Matrix Transpose in Python

I have a matrix, say

A = [[0,0],[1,1]]

and I would like to zip its components to have

(0,1),(0,1)

With two rows in A, this can be obtained easily with

zip(A[0],A[1])

What if I have a matrix A of any dimension

A = [[0,0],[1,1],[2,2]]

How to zip a sequence of elements?

Thanks for your ideas.

Community
  • 1
  • 1
kiriloff
  • 25,609
  • 37
  • 148
  • 229

1 Answers1

16

Use zip(*A).

>>> zip(*A)
[(0, 1, 2), (0, 1, 2)]
BrenBarn
  • 242,874
  • 37
  • 412
  • 384