2

Example:

list1 = [0,1,2]
list2 = [0,1]
list3 = [0,1,2,3]

Then the permutations would be:

0,0,0
0,0,1
0,0,2
0,0,3
0,1,0
0,1,1
0,1,2
0,1,3
1,0,0
1,0,1
1,0,2
1,0,3
1,1,0
1,1,1
1,1,2
1,1,3

...and so on with 3 x 2 x 4 = 24 permutations.

The number of lists is not necessarily 3 (they can be any number, n) and the order matters so 0,0,1 is not the same as 0,1,0.

I understand I might have to use itertools somehow, but not sure how to approach this. I can't just make three nested loops since the number of lists varies.

This is a variation of this question, but the number of lists varies and order matters.

I appreciate any help or hint. Thanks.

Community
  • 1
  • 1
AzureMinotaur
  • 646
  • 2
  • 9
  • 22
  • You didn't find a good duplicate because you're confusing "permutation" with "cartesian product" – Paul Hankin Mar 23 '15 at 05:05
  • Thanks, it is indeed a cartesian product and this is exactly what I wanted. I named it incorrectly as "permutation", my bad. – AzureMinotaur Mar 23 '15 at 05:20
  • 1
    @user2898278 it's not "your bad" - don't worry about it. Sometimes just not knowing the name of something happens. You showed effort in research and showing input/output examples - it's a good question. – Jon Clements Mar 23 '15 at 08:19

1 Answers1

3
list1 = [0,1,2]
list2 = [0,1]
list3 = [0,1,2,3]
listOfLists = [list1,list2,list3]
for list in itertools.product(*listOfLists):
    print(list)  
Soronbe
  • 906
  • 5
  • 12