Python Iterate Two Lists Sequentially

I have three arsenal.deques and also what I should do is to iterate over each of them and perdevelop the exact 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 trying to find some attribute XXX which would ideally permit me to write:

for obj in XXX(deque1, deque2, deque3): some_action(obj)The vital thing here is that XXX have to be effective sufficient - without making copy or silently utilizing range(), etc. I was expecting to discover it in integrated attributes, yet I found nothing similar to it so much.

You watching: Python iterate two lists sequentially

Is there such point already in Python or I have to create a function for that by myself?


*

*

Depfinishing on what order you desire to process the items:

import itertoolsfor 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 prevent hard-coding the actual deques or number of deques:

deques = for item in itertools.chain(*deques): some_action(item)To show the distinction in order of the above methods:

*

The answer is in itertools

iterdevices.chain(*iterables) Make an iterator that retransforms facets from the first iterable till it is exhausted, then proceeds to the next iterable, till every one of the iterables are exhausted. Used for treating consecutive sequences as a single sequence. Equivalent to:

def chain(*iterables): # chain("ABC", "DEF") --> A B C D E F for it in iterables: for element in it: yield facet

*

*

Call me crazy, but why is utilizing itertools thshould 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_activity, (deque1, deque2, deque3)Even crazier: you most likely are going to usage this once. Why not simply do:

for d in (deque1, deque2, deque3): for obj in d: some_action(obj)What"s going on tright here is instantly obvious without having to look at the code/docs for the long-name attribute or having to look up the docs for itertools.something()


If I understand your question correctly, then you have the right to usage map through the first dispute collection to None, and all the other disagreements as your lists to iteprice over.

E.g (from an iPython prompt, however you acquire 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 Hi2 b there3 c world4 d .
Accepts a bunch of iterables, and yields the contents for each of them in sequence.

See more: Oncology Pharmacy Preparatory Review Course 2013, 403 Forbidden

def XXX(*lists): for aList in lists: for item in aList: yield iteml1 = <1, 2, 3, 4>l2 = <"a", "b", "c">l3 = <1.0, 1.1, 1.2>for item in XXX(l1, l2, l3): print item1234abc1.01.11.2
It looks prefer you desire iterdevices.chain:

"Make an iterator that returns aspects from the initially iterable till it is worn down, then proceeds to the next iterable, until all of the iterables are tired. Used for dealing with consecutive sequences as a solitary sequence."


Thanks for contributing a solution to Stack Overflow!

Please be sure to answer the question. Provide details and also share your research!

But avoid

Asking for assist, clarification, or responding to other answers.Making statements based upon opinion; ago them up via recommendations or personal endure.

To learn more, view our tips on writing excellent answers.


Article Your Answer Discard

By clicking “Message Your Answer”, you agree to our terms of organization, privacy plan and cookie policy


Not the answer you're looking for? Browse various other questions tagged python iterator or ask your own question.


site architecture / logo © 2021 Stack Exreadjust Inc; user contributions licensed under cc by-sa. rev2021.8.13.39972


Your privacy

By clicking “Accept all cookies”, you agree Stack Exchange deserve to save cookies on your tool and also discshed indevelopment in accordance with our Cookie Policy.