0

itertools.product() - list of lists (Cartesian product)

I know how to use itertools.product() but I have a problem. This function takes many list separated with comma. I want to use it with list of lists. For example: my_list = [[1,2,3], ['a', 'b', 'c'], ['alpha', 'beta', 'gamma']] and result: (1, 'a', 'alpha'), (1, 'a', 'beta'), (1, 'a', 'gamma'), (1, 'b', 'alpha') ... etc. I don't know how many elements are in my_list, so I cannot do something like this: itertools.product(my_list[0], my_list[1]), my_list[2]) So, will you help me? ;)

8th May 2020, 11:11 AM
Michał Dobranowski
2 ответов
+ 2
Itertools.product(*my_list) And that wasn't easy I tried many other ways but failed and somehow i realized I need to get those elements as they are in list so doing product(my_list[0]) will work but didn't, then I got reminded of * which unpacks the list item :-)
8th May 2020, 11:37 AM
Abhay
Abhay - avatar
+ 1
Have you tried just unpacking my_list inside the 'product'? I've not got my IDE on right now to check if that's possible.
8th May 2020, 11:37 AM
rodwynnejones
rodwynnejones - avatar