-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

arodriguezdonaire
  • 5,396
  • 1
  • 26
  • 50
Jan Klaas
  • 289
  • 7
  • 20

1 Answers1

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']]
Julien Spronck
  • 15,069
  • 4
  • 47
  • 55