Is there a reason zip() wipes out data?

DFS nospam at dfs.com
Mon May 9 00:22:46 EDT 2016


python 2.7.11 docs: "The returned list is truncated in length to the 
length of the shortest argument sequence."


a = ['who','let','the']
b = ['dogs','out?']
c = zip(a,b)

print c
[('who', 'dogs'), ('let', 'out?')]


Wouldn't it be better to return an empty element than silently kill your 
data?

[('who', 'dogs'), ('let', 'out?'), ('the', '')]




Edit: I see they addressed this in 3.5 (maybe earlier), with an option:

"itertools.zip_longest(*iterables, fillvalue=None)

Make an iterator that aggregates elements from each of the iterables. If 
the iterables are of uneven length, missing values are filled-in with 
fillvalue. Iteration continues until the longest iterable is exhausted."




More information about the Python-list mailing list