Is there a built-in function that works like zip() but that will pad the results so that the length of the resultant list is the length of the longest input rather than the shortest input?
>>> a = ['a1']
>>> b = ['b1', 'b2', 'b3']
>>> c = ['c1', 'c2']
>>> zip(a, b, c)
[('a1', 'b1', 'c1')]
>>> What command goes here?
[('a1', 'b1', 'c1'), (None, 'b2', 'c2'), (None, 'b3', None)]
In Python 3 you can use
itertools.zip_longestYou can pad with a different value than
Noneby using thefillvalueparameter:With Python 2 you can either use
itertools.izip_longest(Python 2.6+), or you can usemapwithNone. It is a little known feature ofmap(butmapchanged in Python 3.x, so this only works in Python 2.x).