14

I am trying to write a module to combine a variable number of lists using itertools.product.

The closest I can get is:

import itertools
lists = [["item1","item2"],["A","b","C"], ["etc..."]]
searchterms = list(itertools.product(lists))
print searchterms

This doesn't work, because lists is a single list, so it just returns the original sequence. But I can't figure out how to pass each element of the lists variable to itertools.

Thanks for any suggestions.

M.A.G
  • 559
  • 2
  • 6
  • 21
ralph346526
  • 338
  • 3
  • 12

1 Answers1

23

You need to use * to separate the single list into its constituent lists:

searchterms = list(itertools.product(*lists))

See the Python Tutorial section on Unpacking Argument Lists.

agf
  • 171,228
  • 44
  • 289
  • 238