0

Let's say that I have this list with 3 items and every item contains 3 more items:

list = [ [1,2,3], [4,5,6], [7,8,9] ]

I want to create a 3**2 = 9 list which contains all the combinations of the items, which means

combination of (x,y,z) with x in [1,4,7], y in [2,5,8] and z in [3,6,9], so I will use list comprehension like this:

new_list =[(x,y,z) for x in [1,2,3] for y in [4,5,6] for z in [7,8,9]]

This is a "manual" approach. But if I want to use in my code large lists which every time vary in length, let's say a 20D list (20 items and every item contains 20 more items), how can I create a generic type of code?

1 Answer 1

4

You can use itertools.product but the result would be 33 = 27 item :

>>> from itertools import product
>>> lst = [ [1,2,3], [4,5,6], [7,8,9] ]
>>> 
>>> list(product(*lst))
[(1, 4, 7), (1, 4, 8), (1, 4, 9), (1, 5, 7), (1, 5, 8), (1, 5, 9), (1, 6, 7), (1, 6, 8), (1, 6, 9), (2, 4, 7), (2, 4, 8), (2, 4, 9), (2, 5, 7), (2, 5, 8), (2, 5, 9), (2, 6, 7), (2, 6, 8), (2, 6, 9), (3, 4, 7), (3, 4, 8), (3, 4, 9), (3, 5, 7), (3, 5, 8), (3, 5, 9), (3, 6, 7), (3, 6, 8), (3, 6, 9)] 

Note that when you are dealing with large datasets you don't need to convert the result to list. Since the product returns an iterator you can simple loop overt the products and access to items.

Sign up to request clarification or add additional context in comments.

1 Comment

That was exactly what I was looking for, also you are right about the result, that would be 3**3=27.

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.