11

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
  • 24,401
  • 34
  • 141
  • 212

1 Answers1

14

Use zip(*A).

>>> zip(*A)
[(0, 1, 2), (0, 1, 2)]
BrenBarn
  • 228,001
  • 34
  • 392
  • 371