2

For example I have a list of objects like this:

[[{1},{2},{3}],[{4},{5}],[{6},{7},{8}]]

I need to iterate through them all to get on each iteration objects like:

 1,4,6
 1,4,7
 1,4,8
 1,5,6
 1,5,7
 1,5,8
 2,4,6
 2,4,7
 2,4,8
 2,5,6
 2,5,7
 2,5,8

Basically each result is like a sub array of the input lists.

1 Answer 1

7

You can easily use itertools.product

>>> import itertools
>>> x = list(itertools.product([1,2,3],[4,5],[6,7,8]))
[(1, 4, 6), (1, 4, 7), (1, 4, 8), (1, 5, 6), (1, 5, 7), (1, 5, 8), (2, 4, 6), (2, 4, 7), (2, 4, 8), (2, 5, 6), (2, 5, 7), (2, 5, 8), (3, 4, 6), (3, 4, 7), (3, 4, 8), (3, 5, 6), (3, 5, 7), (3, 5, 8)]

Note that the output of every combination you are looking for is called the Cartesian product of your input lists.

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

Comments

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.