Python @ DjangoSpin

PyPro #76 All possible permutations of a list

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon
Reading Time: 1 minutes

All possible permutations of a list in Python

All possible permutations of a list in Python

Write a Python program to print all possible permutations of a list such as ['a', 'b', 'c'].

All possible permutations of a list

import itertools

listOne = ['a', 'b', 'c']

for permutation in itertools.permutations(listOne):
	print(permutation)

## ('a', 'b', 'c')
## ('a', 'c', 'b')
## ('b', 'a', 'c')
## ('b', 'c', 'a')
## ('c', 'a', 'b')
## ('c', 'b', 'a')

Here's help associated with the constructor of class permutations of itertools.

>>> help(itertools.permutations)
Help on class permutations in module itertools:

class permutations(builtins.object)
 |  permutations(iterable[, r]) --> permutations object
 |  
 |  Return successive r-length permutations of elements in the iterable.
 |  
 |  permutations(range(3), 2) --> (0,1), (0,2), (1,0), (1,2), (2,0), (2,1)
...

See also:

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon

Leave a Reply