DEV Community

qing
qing

Posted on

TIL: Python's `zip()` can accept an optional `strict` parameter

TIL: Python's zip() can accept an optional strict parameter

If you're like me, you've probably used Python's built-in zip() function countless times to iterate over multiple lists in parallel. However, have you ever stopped to think about what happens when the lists are of different lengths? Prior to Python 3.10, zip() would simply stop at the end of the shortest list, potentially leading to silent data loss.

As of Python 3.10, zip() now accepts an optional strict parameter, which defaults to False. When set to True, zip() will raise a ValueError if the input lists are of different lengths.

Here's an example:


python

---

*Follow me on Dev.to for daily Python tips and quick guides!*

---

*🛠️ Useful resource: **Content Creator Ultimate Bundle (Save 33%)** — $29.99. Check it out on Gumroad!*
Enter fullscreen mode Exit fullscreen mode

Top comments (0)