1# A Python program to print all permutations using library function
2from itertools import permutations
3perm = permutations([1, 2, 3])
4for i in list(perm):
5 print (i)
6# (1, 2, 3)
7# (1, 3, 2)
8# (2, 1, 3)
9# (2, 3, 1)
10# (3, 1, 2)
11# (3, 2, 1)
1import itertools
2
3a = [1, 2, 3]
4n = 3
5
6perm_iterator = itertools.permutations(a, n)
7
8for item in perm_iterator:
9 print(item)
1>>> from itertools import permutations
2>>> perms = [''.join(p) for p in permutations('stack')]
3>>> perms
4
1def permute(LIST):
2 length=len(LIST)
3 if length <= 1:
4 yield LIST
5 else:
6 for n in range(0,length):
7 for end in permute( LIST[:n] + LIST[n+1:] ):
8 yield [ LIST[n] ] + end
9
10for x in permute(["3","3","4"]):
11 print x
12