Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Combinations from list of lists

I want to get all the possible combinations like so:

a = [1, 2, 3]
b = [4, 5]
c = [-1]

print(list(product(a, b, c)))

Output:

[(1, 4, -1), (1, 5, -1), (2, 4, -1), (2, 5, -1), (3, 4, -1), (3, 5, -1)]

However, I have all my lists stored inside a list:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

s = [[1,2,3], [4,5], [-1]]

print(list(product(s)))

Output:

[([1, 2, 3],), ([4, 5],), ([-1],)]

I’ve previously tried unpacking the list, but I’ve only been able to create one big list, or a dictionary. Is there another way of unpacking the list or getting the product in the same way as the first example?

>Solution :

This is argument unpacking, demonstsrated in the tutorial at 4.8.5. Unpacking Argument Lists:

>>> print(list(product(*s)))
[(1, 4, -1), (1, 5, -1), (2, 4, -1), (2, 5, -1), (3, 4, -1), (3, 5, -1)]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading