-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
  • 38,521
  • 31
  • 149
  • 235
bruhsmh
  • 321
  • 2
  • 4
  • 12

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
  • 22,114
  • 2
  • 48
  • 60