0

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?

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

I did not know that. I will certainly check it. Thanks anyway.
@genclik27 you're welcome. you can use it with named parameters also, check out my updated answer.
1

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)]
>>>

1 Comment

Very simple. Thanks, this is what I was looking for.

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.