4

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
Red Cricket
  • 9,762
  • 21
  • 81
  • 166
  • 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. – Alejandro Apr 19 '16 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). – Red Cricket Apr 19 '16 at 18:12
  • @Alejandro no; this task is normally called "iterating in parallel" - as in the now-linked canonical duplicate. – Karl Knechtel Sep 25 '22 at 22:18

4 Answers4

8

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"
Csaba Toth
  • 10,021
  • 5
  • 75
  • 121
Alejandro
  • 3,263
  • 2
  • 22
  • 38
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

Nilesh
  • 20,521
  • 16
  • 92
  • 148
  • 1
    might be worth mentioning izip_longest from itertools if op wants to not cut off at the shorter list length https://docs.python.org/2/library/itertools.html#itertools.izip_longest – Garrett R Apr 19 '16 at 17:45
  • @GarrettR, updated note with your suggestions. Thanks, for making StackOverflow better :) – Nilesh Apr 19 '16 at 17:48
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

Jack Wu
  • 447
  • 4
  • 14
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) )
Joakim Ericsson
  • 4,616
  • 3
  • 21
  • 29