-1

I want to switch rows and columns so:

[[1,2,3],[4,5,6],[7,8,9]]

would turn into:

[[1,4,7],[2,5,8],[3,6,9]]

I know my list has a fixed 4rows*4columns. After I switch, I will have a function that manipulates it then switches is back.

I've tried using the zip function but it doesn't seem to work how I want it. Any built in functions or other ways to do this?

Brad Solomon
  • 34,372
  • 28
  • 129
  • 206
bruhsmh
  • 191
  • 2
  • 2
  • 11

1 Answers1

3

Use the common zip idiom:

>>> zip(*[[1,2,3],[4,5,6],[7,8,9]])
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
donkopotamus
  • 20,509
  • 2
  • 42
  • 59