wildling
wildling

Reputation: 33

Print list elements by 1 column and 1 row, then 1 column and 2 row and so on till end of the list

This is my list:

numbs = [[1, 2, 3], [2, 3, 4], [4, 5, 6]]

I want it to print like this:

1 2 4
2 3 5
3 4 6

I have tried using:

for xs in numbs:
    print(" ".join(map(str, xs)))

But it prints this instead:

1 2 3
2 3 4
4 5 6

Upvotes: 3

Views: 57

Answers (1)

user9706
user9706

Reputation:

It looks like you are using python so you are looking for the built-in function zip():

zip(*numbs)
[(1, 2, 4), (2, 3, 5), (3, 4, 6)]

And formatted the way you asked for:

print("\n".join([ " ".join(map(str, l)) for l in zip(*numbs) ])) 
1 2 4
2 3 5
3 4 6

Upvotes: 2

Related Questions