-1

I've been struggling with this for a little while so I think it's time that I asked for help. I have two lists L1, L2 each a certain number of elements long (not necessarily the same length). What is the most efficient (and pythonic) way of calculating all the possible permutations, and then print the results? The simplest way is to do

for l1 in L1:
    for l2 in L2:
        print(l1, l2)

but this doesn't seem very efficient to me either in calculation time or coding. Plus this expression would get very unwieldy if my number of lists grows. The "itertools" module doesn't seem to cover this kind of permutation

Thanks for all help offered!

Dai
  • 345
  • 3
  • 12
  • 1
    itertools.product ? – Neil Mar 25 '19 at 18:56
  • 3
    You are not actually looking for permutation, but rather a cross product of the two list. For that you need [`itertools.product`](https://docs.python.org/3/library/itertools.html#itertools.product). – metatoaster Mar 25 '19 at 18:56

1 Answers1

3

Your code has indeed an itertools equivalent:

for l1, l2 in itertools.product(L1, L2):
    print(l1, l2)
adrtam
  • 6,991
  • 2
  • 12
  • 27