What's the difference between these?

>>> zipped = zip(iter1, iter2)
>>> mapped = map(None, iter1, iter2)

The answer turns out to be depend on whether iter1 and iter2 are the same length. Pretty subtle: zipped is truncated to the length of the shortest of (iter1, iter2). mapped is the same length as the longest of (iter1, iter2), and missing items from the shorter iterable are replaced by None.

>>> x = range(5)
>>> y = range(10, 13)
>>> zip(x, y)
[(0, 10), (1, 11), (2, 12)]
>>> map(None, x, y)
[(0, 10), (1, 11), (2, 12), (3, None), (4, None)]

This seems like not good API design to me. I'd rather see an explicit keyword arg like zip(iter1, iter2, ... truncate=True).


Comments

comments powered by Disqus