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
  • 545
  • 5
  • 16
  • 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
  • 28,235
  • 9
  • 60
  • 81
deadshot
  • 8,881
  • 4
  • 20
  • 39
-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
  • 932
  • 1
  • 7
  • 19