My list is[1,1,1,2,2,2] and the total combinations are 20 including input list The output should be like [1,1,2,1,2,2] [1,2,1,2,1,2] So on Up to 20 combinations The combinations must not be repititive Someone pls help me to find a solution
Asked
Active
Viewed 31 times
-1
-
Show us what you have tried so far? – pauli Mar 25 '18 at 15:34
-
Possible duplicate of [How to generate all permutations of a list in Python](https://stackoverflow.com/questions/104420/how-to-generate-all-permutations-of-a-list-in-python) – Primusa Apr 07 '18 at 04:06
1 Answers
1
Python by default provides methods to help us find the permutations and combinations of a sequence. They come in a package known as itertools
.
first you will have to import
permutations
:from itertools import permutations
Assuming that your list is stored in
x
i.e
x=[1,1,1,2,2,2]
Take any other variable. eg:
P
P= permutations(x)
[
P
is basically a list of all the different permutations of the listx
]for i in list(P): print(i)
You'll have the different combinations.
Source: GeeksforGeeks

Thomas Fritsch
- 9,639
- 33
- 37
- 49

Simran Koul
- 11
- 3
-
That's so informative,but do you have any idea about the source code of permutations function ,i want to know more about it – rakesh Mar 25 '18 at 16:25