3

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.

1 Answer 1

6

Using itertools.izip_longest:

for a, b in izip_longest(l1, l2, fillvalue=''):
    print "{0:20s}\t{1:20s}".format(a, b)
Sign up to request clarification or add additional context in comments.

2 Comments

+1. I might suggest as kwarg to izip_longest: fillvalue="" instead. But what you have is quite explicit which is of course the ultimate goal.
@Adam: I just read the documentation, but it didn't click. Thanks, fixed it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.