On this page
itertools.zip_longest
Prev: itertools.tee | Up: itertools
Rel: itertools.accumulate, itertools.batched, itertools.chain, itertools.combinations
Ref: docs.python.org — itertools.zip_longest
Tags: #public #python3 #stdlib #itertools
Import
import itertools
from itertools import zip_longest
Signature & Overview
itertools.zip_longest(*iterables, fillvalue=None)
Return a zip_longest object whose .next() method returns a tuple where the i-th element comes from the i-th iterable argument. The .next() method continues until the longest iterable in the argument sequence
Usage
from itertools import zip_longest
assert list(zip_longest("AB", [1, 2, 3], fillvalue=0)) == [("A", 1), ("B", 2), (0, 3)]
Gotchas
- Continues until the longest iterable is exhausted: Unlike built-in
zip()(which stops at the shortest), shorter inputs are padded withfillvalue(defaultNone). - Avoid mutable
fillvalue: The exact samefillvalueobject is reused for every missing slot—never passfillvalue=[]if you plan to mutate the filled slots.
REPL Inspect
from itertools import zip_longest
help(zip_longest)
Linked from
Related notes
- tuts/python3/stdlib_module/itertools/tee Linked here; links to this page; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/itertools/accumulate Linked here; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/batched Linked here; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/chain Linked here; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/combinations Linked here; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/combinations_with_replacement shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/compress shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/count shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/cycle shared tags: #itertools, #stdlib