Title: An elegant and fast way to consecutively iterate over two or more containers in Python?
Tags: python;iterator
Question: 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?
Comment: AFAIK the zip() function is not what I need here.

Comment: Thanks, David, that does make sense

Comment: The subject didn't make sense to me so I changed "consequently" into "consecutively". Please rollback if I'm just ignorant!

Comment: Have you looked at the zip function?

Here is the accepted answer: Depending on what order you want to process the items:
import itertools
for items in itertools.izip(deque1, deque2, deque3):
for item in items:
some_action(item)
for item in itertools.chain(deque1, deque2, deque3):
some_action(item)
I'd recommend doing this to avoid hard-coding the actual deques or number of deques:
deques = [deque1, deque2, deque3]
for item in itertools.chain(*deques):
some_action(item)
To demonstrate the difference in order of the above methods:
&gt;&gt;&gt; a = range(5)
&gt;&gt;&gt; b = range(5)
&gt;&gt;&gt; c = range(5)
&gt;&gt;&gt; d = [a, b, c]
&gt;&gt;&gt;
&gt;&gt;&gt; for items in itertools.izip(*d):
...     for item in items:
...         print item,
0 0 0 1 1 1 2 2 2 3 3 3 4 4 4
&gt;&gt;&gt;
&gt;&gt;&gt; for item in itertools.chain(*d):
...     print item,
0 1 2 3 4 0 1 2 3 4 0 1 2 3 4
&gt;&gt;&gt;

Here is another answer: 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."

Here is another answer: How about zip?
for obj in zip(deque1, deque2, deque3):
for sub_obj in obj:
some_action(sub_obj)

Here is another answer: Call me crazy, but why is using itertools thought to be necessary? What's wrong with:
def perform_func_on_each_object_in_each_of_multiple_containers(func, containers):
for container in containers:
for obj in container:
func(obj)
perform_func_on_each_object_in_each_of_multiple_containers(some_action, (deque1, deque2, deque3)
Even crazier: you probably are going to use this once. Why not just do:
for d in (deque1, deque2, deque3):
for obj in d:
some_action(obj)
What's going on there is immediately obvious without having to look at the code/docs for the long-name function or having to look up the docs for itertools.something()
Comment for this answer: Why reinvent the wheel? This is something that comes up a lot.

Here is another answer: I would simply do this :
for obj in deque1 + deque2 + deque3:
some_action(obj)
Comment for this answer: But then you're concatting those lists into a megalist. That's a giant time-drain.

Here is another answer: Accepts a bunch of iterables, and yields the contents for each of them in sequence.
def XXX(*lists):
for aList in lists:
for item in aList:
yield item
l1 = [1, 2, 3, 4]
l2 = ['a', 'b', 'c']
l3 = [1.0, 1.1, 1.2]
for item in XXX(l1, l2, l3):
print item

Here is another answer: Use itertools.chain(deque1, deque2, deque3)

Here is another answer: The answer is in itertools
itertools.chain(*iterables)
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. Equivalent to:
def chain(*iterables):
# chain('ABC', 'DEF') --&gt; A B C D E F
for it in iterables:
for element in it:
yield element

Here is another answer: &gt;&gt;&gt; a = [[],[],[]]
&gt;&gt;&gt; b = [[],[],[]]
&gt;&gt;&gt; for c in [*a,*b]:
c.append(&quot;derp&quot;)
&gt;&gt;&gt; a
[['derp'], ['derp'], ['derp']]
&gt;&gt;&gt; b
[['derp'], ['derp'], ['derp']]
&gt;&gt;&gt;
Comment for this answer: Welcome to Stack Overflow. While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. [How to Answer](https://stackoverflow.com/help/how-to-answer)
Comment for this answer: Please consider adding an explanation to your answer

Here is another answer: If I understand your question correctly, then you can use map with the first argument set to None, and all the other arguments as your lists to iterate over.
E.g (from an iPython prompt, but you get the idea):
In [85]: p = [1,2,3,4]
In [86]: q = ['a','b','c','d']
In [87]: f = ['Hi', 'there', 'world', '.']
In [88]: for i,j,k in map(None, p,q,f):
....:     print i,j,k
....:
....:
1 a Hi
2 b there
3 c world

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 10, 'CommentCount': 4, 'ContentLicense': 'CC BY-SA 2.5', 'CreationDate': '2011:00:01/25/11', 'FavoriteCount': 7.0, 'PostTypeId': 1, 'Tags': '<python><iterator>', 'Title': 'An elegant and fast way to consecutively iterate over two or more containers in Python?', 'ViewCount': 21905, 'id': 4795772, 'user_AboutMe': '', 'user_AccountId': 233741.0, 'user_DisplayName': 'lithuak', 'user_Reputation': 5738.0, 'user_Views': 169.0, 'user_id': 499439.0}}