DEV Community

Toolloom
Toolloom

Posted on • Originally published at toolloom.com on

Safer Looping with Python's Strict Zip

We all love using Python's zip() function to loop through multiple lists at the same time. But here is a silent bug waiting to happen: by default, if your lists are of different lengths, zip() will quietly stop at the end of the shortest list, completely ignoring the extra elements in the longer one.

Starting in Python 3.10, you can pass the strict=True argument (like zip(names, ages, strict=True)). This forces Python to raise a ValueError if the iterables aren't of equal length. It's a tiny addition that saves you from hours of debugging silent data omissions down the road.

Top comments (0)