r/Python • u/Nausicaa23Pencil • 3h ago
Discussion zip(strict=True) catches mismatched lengths, but potentially much later than you'd want
I've been adding strict=True to quite a few zip() calls in our codebase and ran into a subtle distinction that's worth keeping in mind.
Consider something like:
for user, email in zip(users, emails, strict=True):
send_email(user, email)If the iterables have different lengths, Python will raise a ValueError. But by that point you might have already sent hundreds of emails.
Obviously this is consistent with how lazy iteration works. Still, I think the name "strict" can give a slightly misleading sense of safety when you're reading code that performs side effects.
For small inputs, materializing the zipped pairs before processing them solves this particular issue. For large streams, things get more interesting.
I've started distinguishing between validation that happens before processing and validation that happens during processing when reviewing iterator-heavy Python code.
Are there other standard library features where this distinction has caught you out?