I have three collection.deques and what I need to do is to iterate over each of them and perform the same action:
for obj in deque1:
some_action(obj)
for obj in deque2:
some_action(obj)
for obj in deque3:
some_action(obj)
I'm looking for some function XXX which would ideally allow me to write:
for obj in XXX(deque1, deque2, deque3):
some_action(obj)
The important thing here is that XXX have to be efficient enough - without making copy or silently using range(), etc. I was expecting to find it in built-in functions, but I found nothing similar to it so far.
Is there such thing already in Python or I have to write a function for that by myself?
Use
itertools.chain(deque1, deque2, deque3)
Depending on what order you want to process the items:
I'd recommend doing this to avoid hard-coding the actual deques or number of deques:
To demonstrate the difference in order of the above methods:
The answer is in itertools
I would simply do this :
It looks like you want itertools.chain:
"Make an iterator that returns elements from the first iterable until it is exhausted, then proceeds to the next iterable, until all of the iterables are exhausted. Used for treating consecutive sequences as a single sequence."
Accepts a bunch of iterables, and yields the contents for each of them in sequence.