Reputation: 323
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?
Upvotes: 0
Views: 1958
Reputation: 29416
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
Upvotes: 2
Reputation: 368904
Use argument unpacking:
>>> lis = [[1,2,3],[6,7,8],[2,4,5]]
>>> list(itertools.product(*lis))
[(1, 6, 2), (1, 6, 4), (1, 6, 5), (1, 7, 2), (1, 7, 4), (1, 7, 5), (1, 8, 2),
(1, 8, 4), (1, 8, 5), (2, 6, 2), (2, 6, 4), (2, 6, 5), (2, 7, 2), (2, 7, 4),
(2, 7, 5), (2, 8, 2), (2, 8, 4), (2, 8, 5), (3, 6, 2), (3, 6, 4), (3, 6, 5),
(3, 7, 2), (3, 7, 4), (3, 7, 5), (3, 8, 2), (3, 8, 4), (3, 8, 5)]
>>>
Upvotes: 1