Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print a table from lists with different lengths in Python

How do I print a table from two lists that have varying lengths (each list being a column)?

Example:

>>> l1=['Cat', 'Dog', 'Gorilla', 'Ladybug']
>>> l2=['Cat', 'Dog']
>>> print_chart(l1, l2)
Cat        Cat
Dog        Dog
Gorilla
Ladybug

Using rjust may be useful.

like image 884
paragbaxi Avatar asked Dec 13 '25 00:12

paragbaxi


1 Answers

Using itertools.izip_longest:

for a, b in izip_longest(l1, l2, fillvalue=''):
    print "{0:20s}\t{1:20s}".format(a, b)
like image 166
Björn Pollex Avatar answered Dec 14 '25 13:12

Björn Pollex