Printing lists to tables in python

If I had three lists, for example

a = [1, 2, 3] b = [4, 5, 6] c = [7, 8, 9] 

And I wanted to print it as follows

 1 4 7 2 5 8 3 6 9 

How can I do it?

+4
source share
3 answers

The hard part of this is array transfer. But it is easy with zip :

 a = [1, 2, 3] b = [4, 5, 6] c = [7, 8, 9] t = zip(a, b, c) 

Now you just print it:

 print('\n'.join(' '.join(map(str, row)) for row in t)) 
+8
source

This should do it:

 '\n'.join(' '.join(map(str,tup)) for tup in zip(a,b,c)) 
+6
source

With a generator expression list without display function:

 '\n'.join(' '.join(str(y) for y in x) for x in zip(a,b,c)) 
+2
source

Source: https://habr.com/ru/post/1482026/


All Articles