Python Glossary Русский

Python 3.10+Loopsfunction

zip(strict=True)

iteration

The strict=True parameter (Python 3.10+) makes zip() raise ValueError if the iterables passed have different lengths. Without strict= zip silently stops at the shortest one.

Syntax

zip(*iterables, strict=False)

Turn on strict=True whenever equal length is an invariant you rely on: a silently dropped tail becomes a loud error right where the bug is. Note the check is lazy — the ValueError fires not when you call zip() but mid-iteration, the moment the shorter iterable runs out.

Examples

print(list(zip([1,2],["a","b"])))      # → [(1, 'a'), (2, 'b')]
print(list(zip([1,2,3],["a","b"])))    # → [(1, 'a'), (2, 'b')]  (3 потеряна)
try:
    list(zip([1,2,3],["a","b"], strict=True))
except ValueError as e:
    print("ValueError:", e)  # → ValueError: zip() argument 2 is shorter than argument 1
print(list(zip([1,2],["a","b"], strict=True)))  # → [(1, 'a'), (2, 'b')]

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy