Search code examples
pythonpython-2.7python-itertoolscartesian-product

generating tuples using n-lists with itertools.product


How can I use itertools.product function if I don't know the number of the lists? I have list and it has lists inside of it.

Like,

lis = [[1,2,3],[6,7,8],[2,4,5]]

Normally I need to do,

product([1,2,3],[6,7,8],[2,4,5])

How do I do that if the input is a list like in the example?


Solution

  • Try the following:
    product(*lis)
    It's called argument unpacking.
    Short note: you can use argument unpacking with named parameters also, with double star:

    def simpleSum(a=1,b=2):
        return a + b
    simpleSum(**{'a':1,'b':2}) # returns 3