5

I am look like to be able to iterate over two arrays in parallel (or with only one for loop).

Here is my script I tried ...

#!/usr/bin/env python list1 = [ 'one', 'two', 'three' ] list2 = [ 'I', 'II', 'III', 'IV', 'V' ] for word in list1: print word + " from list1" for roman in list2: print roman + " from list2" for ( word, roman ) in (list1 list2): if word: print word + " from list1" if roman: print roman + " from list2" 

But is obviously incorrect as I get a syntax error:

 File "./twoarr.py", line 12 for ( word, roman ) in (list1 list2): ^ SyntaxError: invalid syntax 

I am trying to get output that would look like this:

one from list1 I from list2 two from list1 II from list2 three from list1 III from list2 IV from list2 V from list2 
3
  • I suggest to change the title of this question. It seems to me that you are not looking to do it in parallel. You want to do it in a single loop. The title might suggest a different thing. Commented Apr 19, 2016 at 18:04
  • Maybe so. But to me parallel does not always imply multitasking (forking, child parent processes etc.). I meant "parallel" as opposed to processing the arrays serially (one after another). Commented Apr 19, 2016 at 18:12
  • @Alejandro no; this task is normally called "iterating in parallel" - as in the now-linked canonical duplicate. Commented Sep 25, 2022 at 22:18

4 Answers 4

9

To iterate over multiple list you can use the built in function zip. According to the Documentation, this function returns a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. So, applied to your particular example

list1 = [ 'one', 'two', 'three' ] list2 = [ 'I', 'II', 'III', 'IV', 'V' ] for word in list1: print word + " from list1" for roman in list2: print roman + " from list2" for word, roman in zip(list1, list2): print word + " from list1" print roman + " from list2" 

The only drawback of zip is that when your lists, as in this example, have not equal length, zip will return a list of tuples, each with dimension equal to the smaller one. To favour the longest one, and fill with None when necessary, just replace zip with itertools.izip_longest:

from itertools import izip_longest list1 = [ 'one', 'two', 'three' ] list2 = [ 'I', 'II', 'III', 'IV', 'V' ] for word in list1: print word + " from list1" for roman in list2: print roman + " from list2" for word, roman in izip_longest(list1, list2): print word + " from list1" print roman + " from list2" 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! That works great. Just one nit picky point. You left out the ',' in the izip_longest() call. I used for word, roman in izip_longest(list1, list2):
Yes, was a typo. I just fixed it.
7

You can use zip to iterate over 2 list.

>>> list1 = [ 'one', 'two', 'three' ] >>> list2 = [ 'I', 'II', 'III', 'IV', 'V' ] >>> for (x, y) in zip(list1, list2): ... print x + " from list1" ... print y + " from list2" ... one from list1 I from list2 two from list1 II from list2 three from list1 III from list2 

Note: zip will provide till the list which is small. So in your case, list1 has 3 element and list2 has 5 elements, so zip will give data till 3 elements only. you can use izip_longest to reach all element in list2

2 Comments

might be worth mentioning izip_longest from itertools if op wants to not cut off at the shorter list length docs.python.org/2/library/itertools.html#itertools.izip_longest
@GarrettR, updated note with your suggestions. Thanks, for making StackOverflow better :)
3

I have similar requirements, implemented by

for i in range(len(list1)): print list1[i], list2[i] 

needs more detection if lists do not have the same length, or to use some try/except statement to avoid invalid index

Comments

1

If you want it to not just blend the two list but truly run in parallell use two threads:

import _thread def print_list(name, list): for word in list: print(word + " from " + name + "\r\n"); list1 = [ 'one', 'two', 'three' ] list2 = [ 'I', 'II', 'III', 'IV', 'V' ] _thread.start_new_thread( print_list, ("list1",list1) ) _thread.start_new_thread( print_list, ("list2",list2) ) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.