Is it possible to perform multiple loops simultaneously in python.
Like(syntax error, of course):
for a,b in list_of_a,list_of_b:
//do some thing
By simultaneously, I am not meaning the thread or process sense.
I mean, they share the same index or cursor during the iteration.
What I can think of achieving that is:
I am just wondering if there some built-in functions or simpler syntax to achieve that.
for a,b in zip(list_of_a, list_of_b):
# Do some thing
If you're using Python 2.x, are worried about performance, and/or using iterators instead of lists, consider itertools.izip instead of zip.
In Python 3.x, zip replaces itertools.izip; use list(zip(..)) to get the old (2.x) behavior of zip returning a list.
import itertools
for a, b in itertools.izip(list_a, list_b):
# ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With