user2553377
user2553377

Reputation: 25

How to loop over the items of multiple list simultaneously based on the indices

I will like to have your ideas about solving this looping problem on multiple lists. I have multiple lists of different lengths like: ['dog','cat'], [5,45,8,9], ['yellow','blue','black'].

I want to be be able to do something like access the elements of these lists like this:

1. ['dog', 5, 'yellow']
2. ['dog', 5, 'blue']
3. ['dog', 5, 'black']
4. ['dog', 45, 'yellow']
5. ['dog', 45, 'blue']
6. ['dog', 45, 'black']
7. ['dog', 8, 'yellow']
8. ['dog', 8, 'blue']
9. ['dog', 8, 'black']
10. ['dog', 9, 'yellow']
11. ['dog', 9, 'blue']
12. ['dog', 9, 'black']
13. ['cat', 5, 'yellow']
14. ['cat', 5, 'blue']
15. ['cat', 5, 'black']
16. ['cat', 45, 'yellow']
17. ['cat', 45, 'blue']
18. ['cat', 45, 'black']
19. ['cat', 8, 'yellow']
20. ['cat', 8, 'blue']
21. ['cat', 8, 'black']
22. ['cat', 9, 'yellow']
23. ['cat', 9, 'blue']
24. ['cat', 9, 'black']

Thanks. I have to repost it because the formatting makes it clumsy and a little bit unreadable

Upvotes: 1

Views: 99

Answers (2)

Floris
Floris

Reputation: 46415

A slightly more "pedestrian" way to loop over the different lists and make a combined list:

animal = ['dog','cat']
number = [5,45,8,9]
color = ['yellow','blue','black']
num = 0
combined = []

for a in animal:
  for n in number:
    for c in color:
      print a, n, c
      combined.append([a, n, c])

print combined

Saving this exact code in a file animal.py, and running it from the command line with python animal.py results in the following output:

dog 5 yellow
dog 5 blue
dog 5 black
dog 45 yellow
dog 45 blue
dog 45 black
dog 8 yellow
dog 8 blue
dog 8 black
dog 9 yellow
dog 9 blue
dog 9 black
cat 5 yellow
cat 5 blue
cat 5 black
cat 45 yellow
cat 45 blue
cat 45 black
cat 8 yellow
cat 8 blue
cat 8 black
cat 9 yellow
cat 9 blue
cat 9 black
[['dog', 5, 'yellow'], ['dog', 5, 'blue'], ['dog', 5, 'black'], ['dog', 45, 'yellow'], ['dog', 45, 'blue'], ['dog', 45, 'black'], ['dog', 8, 'yellow'], ['dog', 8, 'blue'], ['dog', 8, 'black'], ['dog', 9, 'yellow'], ['dog', 9, 'blue'], ['dog', 9, 'black'], ['cat', 5, 'yellow'], ['cat', 5, 'blue'], ['cat', 5, 'black'], ['cat', 45, 'yellow'], ['cat', 45, 'blue'], ['cat', 45, 'black'], ['cat', 8, 'yellow'], ['cat', 8, 'blue'], ['cat', 8, 'black'], ['cat', 9, 'yellow'], ['cat', 9, 'blue'], ['cat', 9, 'black']]

If you want a specific element, you can now do (for example)

print "element 3 is ", combined[3]

This will return

element 3 is  ['dog', 45, 'yellow']

Upvotes: 2

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 251041

You're looking for itertools.product:

>>> from itertools import product
>>> animal = ['dog','cat']
>>> number = [5,45,8,9]
>>> color = ['yellow','blue','black']
for i,p in enumerate(product(animal, number, color),1):
    print "{}. {}".format(i,str(list(p)))
...     
1. ['dog', 5, 'yellow']
2. ['dog', 5, 'blue']
3. ['dog', 5, 'black']
4. ['dog', 45, 'yellow']
5. ['dog', 45, 'blue']
6. ['dog', 45, 'black']
7. ['dog', 8, 'yellow']
8. ['dog', 8, 'blue']
9. ['dog', 8, 'black']
10. ['dog', 9, 'yellow']
11. ['dog', 9, 'blue']
12. ['dog', 9, 'black']
13. ['cat', 5, 'yellow']
14. ['cat', 5, 'blue']
15. ['cat', 5, 'black']
16. ['cat', 45, 'yellow']
17. ['cat', 45, 'blue']
18. ['cat', 45, 'black']
19. ['cat', 8, 'yellow']
20. ['cat', 8, 'blue']
21. ['cat', 8, 'black']
22. ['cat', 9, 'yellow']
23. ['cat', 9, 'blue']
24. ['cat', 9, 'black']

Upvotes: 7

Related Questions