Patternite Logo

Simultaneously iterating over multiple lists in Python


Using the built-in `zip` generator, we can iterate over the same index of multiple lists at the same time


The zip generator provides a simple way to iterate over multiple lists at the same time. In the above examples, we use zip with two lists, however any number of lists can be used.

The anti-pattern would be to keep track of a counter variable, increment it each iteration, and use it to index each list - an approach that is more verbose and error prone than using zip.

Profile picture for duncster

| edited

Patternite © 2022

Patternite Logo