-1

Here are two examples of lists

list = [["mo", "thu"], ["we", "fri"], ["mo", "mo"]]
list2 = [["mo", "fri", "fri"], ["we", "we"]]

So these lists come in random order. I've given two examples here. What I want to do is compute all possible ways these lists can be permutated. On the level where it's list[1] * list[2] * list[3]. All possible combinations. A smaller example what I would like to reach is this:

list3 = [["we", "thu"],["fri", "thu"]]

-->

[["we", "fri"], ["we", "thu"], ["thu", "fri"], ["thu", "thu"]]

Also, the lists are random so the amount of elements in the list or the nested lists can vary. I can python this out by alot of code but I was hoping there is an easier way to this.

Cheers

2
  • Based on your list3 example, I assume order doesn't matter? Commented Nov 17, 2015 at 12:49
  • No, order does not matter Commented Nov 17, 2015 at 13:00

1 Answer 1

8

What you want is unclear but maybe what you want is the cartesian product of your list? If so, it is very simple:

list3 = [["we", "thu"],["fri", "thu"]]
import itertools
final_list = [list(v) for v in itertools.product(*list3)]
## [['we', 'fri'], ['we', 'thu'], ['thu', 'fri'], ['thu', 'thu']]
Sign up to request clarification or add additional context in comments.

1 Comment

That's exactly what I wanted. Thanks alot!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.