0

Consider the following list:

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

How can I achieve this printing pattern?

1 4 7
2 5 8
3 6 9

More specifically, how can I do it in general for any number of elements in L while assuming that all the nested lists in L have the same length?

MikeKatz45
  • 515
  • 4
  • 12
  • 1
    Does this answer your question? [Transpose list of lists](https://stackoverflow.com/questions/6473679/transpose-list-of-lists) – roganjosh Aug 21 '20 at 18:19

2 Answers2

3

Try this:

L = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
for x in zip(*L):
    print(*x)

Output:

1 4 7
2 5 8
3 6 9
wjandrea
  • 23,210
  • 7
  • 49
  • 68
deadshot
  • 8,317
  • 4
  • 15
  • 36
-1
L = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

    
for i in zip(*L):        
    print(*i)

Produces:

1 4 7
2 5 8
3 6 9

(*i) says to do all of the arguments inside of i, however many there are.

user2415706
  • 781
  • 1
  • 6
  • 15