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:
- Use a int variable to act as a shared cursor
- put them in a list of tuples and iterate the tuple-list. But creating the list is laborious
I am just wondering if there some built-in functions or simpler syntax to achieve that.
If you’re using Python 2.x, are worried about performance, and/or using iterators instead of lists, consider
itertools.izipinstead ofzip.In Python 3.x,
zipreplacesitertools.izip; uselist(zip(..))to get the old (2.x) behavior ofzipreturning a list.