alex
alex

Reputation: 3118

pass list of iterables to itertools function

i am using the itertools.product function. i have a 2-deep nested list, which is a list of iterables. i want to pass this to product function dont know how to format it correctly.

to be clear, i want

In [37]: [k for k in product([1,2],['a','b'])]
Out[37]: [(1, 'a'), (1, 'b'), (2, 'a'), (2, 'b')]

but generated from the a nested_list input like this

nested_list = [[1,2],['a','b']]

but instead i get

In [36]: [k for k in product(nested_list)]
Out[36]: [([1, 2],), (['a', 'b'],)]

Upvotes: 5

Views: 1148

Answers (1)

Cat Plus Plus
Cat Plus Plus

Reputation: 129764

product takes variable number of arguments, so you need to unpack your list.

list(product(*nested_list)) # without list() normally, of course

Upvotes: 7

Related Questions