Skip to main content

How to Make zip Raise an Error for Lists of Different Lengths with strict=True

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

zip() pairs up items from two lists, but when one list is shorter it simply stops, and the extra items vanish without any message. When the lists should always match, that silence hides a bug. strict=True turns it into an error. This guide shows both on Python 3.15.2.

Syntax​

Syntax
zip(<list_a>, <list_b>, strict=True)
PartWhat it means
<list_a>, <list_b>The lists to pair up, item by item
strict=TrueRaise a ValueError if one list runs out before the others

The Python documentation recommends it: "zip() is often used in cases where the iterables are assumed to be of equal length. In such cases, it's recommended to use the strict=True option."

The Example​

Three names but only two ages: an age is missing. The script pairs them first without strict, then with it.

Prerequisites​

  • uv and Python 3.10 or later, the version that added strict. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

strict.py
names = ["Ada", "Grace", "Linus"]
ages = [36, 45]

print(list(zip(names, ages)))
print(list(zip(names, ages, strict=True)))

The strict.py script in a macOS-style code window: three names, two ages, then list(zip(names, ages)) printed, then the same with strict=True.

Step 2: Run It​

Terminal
uv run python strict.py
Output
[('Ada', 36), ('Grace', 45)]
Traceback (most recent call last):
File "/Users/you/source/python-basics/strict.py", line 5, in <module>
print(list(zip(names, ages, strict=True)))
~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: zip() argument 2 is shorter than argument 1

The terminal after uv run python strict.py: [('Ada', 36), ('Grace', 45)] without Linus, then a ValueError: zip() argument 2 is shorter than argument 1 from the strict line.

The first line shows the quiet behaviour: Linus is gone, and nothing says so. The second line stopped with ValueError: zip() argument 2 is shorter than argument 1: argument 2 is ages, and it ran out first. The command works unchanged in PowerShell.

When the second list is the longer one, the message says so: zip([1, 2, 3], [1, 2, 3, 4], strict=True) raised ValueError: zip() argument 2 is longer than argument 1.

Common Mistakes​

MistakeWhat happensDo this instead
Using plain zip for lists that must matchA missing item silently drops a row of your resultAdd strict=True
Expecting strict=True to fill in the missing itemsIt only raises an errorTo pad the shorter list instead, use itertools.zip_longest()

FAQ​

When does the error happen?​

When the loop reaches the end of the shorter list, not when zip is called: zip is lazy. The pairs before that point have already been produced.

Is there a way to fill the gaps instead of failing?​

Yes: the documentation says shorter iterables "can be padded with a constant value to make all the iterables have the same length. This is done by itertools.zip_longest()."

Conclusion​

Without strict, zip dropped Linus without a word. With strict=True, the same lists raised ValueError: zip() argument 2 is shorter than argument 1, which is what you want when the lists are supposed to match.

Additional Resources​

  • zip() — the built-in function and its strict option.
  • PEP 618 — the proposal that added strict to zip.