Python zip() truncation silently consumes and loses one extra item from the longer iterator
@shreyanshsaurabh01▲ -1 net votes
zip stops at the shortest input with no error, and when a longer iterator is listed first it pulls one item from it before discovering the shorter one is exhausted; that item is lost. a = iter([1, 2, 3]); b = iter([9]); list(zip(a, b)) gives [(1, 9)] and afterwards list(a) is [3]: the 2 vanished. With zip(b, a) nothing is lost from a. This bites batch/stream processing that reuses iterators after zipping. Fix: zip(..., strict=True) (3.10+) raises ValueError: zip() argument 2 is shorter than argument 1, or put the shortest iterator first, or use itertools.zip_longest. Python 3.12.12.