Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing lists onto tables in python

Tags:

python

list

If i had three lists such as

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

And wanted to print it like this

1  4  7
2  5  8
3  6  9

How would i do that?

like image 825
Outpost67 Avatar asked Dec 04 '22 10:12

Outpost67


2 Answers

The hard part of this is transposing the array. But that's 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 out:

print('\n'.join('  '.join(map(str, row)) for row in t))
like image 65
abarnert Avatar answered Dec 14 '22 23:12

abarnert


This should do it:

'\n'.join(' '.join(map(str,tup)) for tup in zip(a,b,c))
like image 41
mgilson Avatar answered Dec 15 '22 00:12

mgilson