0

Here is a problem I seem to be stuck on. I am trying to get python to print every combination of two lists.

# Test use case: This does what I expect:
lista = ['1', '2', '3', '4']
listb = ['a', 'b', 'c']

for x in lista:
  for y in listb:
    print  x, y
##
##  result summary -
## 1 a
## 1 b
## 1 c
## 2 a
## 2 b
## 2 c
## 3 a
## 3 b
## 3 c
## 4 a
## 4 b
## 4 c

# actual use case:
# test files:
##    file_a contents =
##    this is group 1:
##    this is group 2:
##    this is group 3:
##    
##    
##    file_b contents =
##    red,1,1,1
##    blue,2,2,2
##    green,3,3,3
##    yellow,4,4,4
##    
import csv
with open('file_b', 'r') as f:
    reader = csv.reader(f)
    with open('file_a', 'r') as template:
      for line in template:
        for row in reader:
          print line, row[0]

The result starts out like (what I want) above, but it only iterates through the first line of file_a and stops.

Any suggestions? Thoughts on why the behavior is different from case A to B?

I've been trying out itertools also, but it treats each character as an an individual string.

Thanks!!

2
  • You have file_a in the inner loop. I think you meant for it to be the outer loop? Commented Aug 6, 2014 at 21:02
  • 1
    reader is an iterator here, so it'll be exhausted after the first iteration. Write f.seek(0);reader = csv.reader(f) between the two loops. Commented Aug 6, 2014 at 21:07

1 Answer 1

3
from itertools import product
lista = ['1', '2', '3', '4']
listb = ['a', 'b', 'c']
print (list(product(lista,listb)))

In [8]: lista = ['1', '2', '3', '4']

In [9]: listb = ['a', 'b', 'c']

In [10]: prod = (product(lista,listb))

In [11]: for x,y in prod:
   ....:         print (x,y)
   ....:     
1 a
1 b
1 c
2 a
2 b
2 c
3 a
3 b
3 c
4 a
4 b
4 c

itertools.product will do the work for you.

Cartesian product of input iterables. Equivalent to nested for-loops in a generator expression. For example, product(A, B) returns the same as ((x,y) for x in A for y in B). This is basically the same as doing:

[(x,y) for x in lista for y in listb]

You can put your lines in two lists and do the same:

with open('file_b', 'r') as f,open('file_a', 'r') as template:
    lines = template.readlines()
    lines2 =  list(csv.reader(f))        
    prod = (product(lines,lines2))
    for x,y in prod:
        print (x,y)
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks so much for all of the suggestions.
@Ashwini Chaudhary - your solution works and I did not need to import from another module.
Padraic Cunningham - very nice solution with product from itertools! I was starting to work in this direction however your solution provided a very powerful answer to the situation at hand.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.