Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
I have a list of lists with a various number of elements (int). I want to print/write it, but in columns rather than in rows.
Example:
l = [[1,2,3],[4,5],[6,7,8,9],[0]]
Result:
1 4 6 0 2 5 7 . 3 . 8 . . . 9 .
The easiest way to do this is to use itertools.izip_longest():
itertools.izip_longest()
for x in itertools.izip_longest(*l, fillvalue="."): print " ".join(str(i) for i in x)
Add a comment
This:
import itertools l = [[1,2,3],[4,5],[6,7,8,9],[0]] for t in itertools.izip_longest(*l): print "".join("%3d" % x if x is not None else " ." for x in t)
produces:
Required, but never shown
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.
Explore related questions
See similar questions with these tags.