C++ - Q&A

Permutation - Combination in Python



One of the key advantage of using Python as compared to other programming languages is that it has extensive library support. It has built-in package to find permutations and combinations of a sequence.

Permutation


First of all, import itertools package for permutations method. This method takes iterable as parameter and returns list object containing each permutation as a tuple. An iterable object can be any data structure like list, tuple, set, string, dictionary and range iterable. Please note that, the iterable must have no repeating elements.

Syntax

permutations(iterable, r)

Parameters

iterable Required. iterable object like list, tuple, set, string , dictionary and range() etc. with no repeating element
r Optional. number of items to permute. must be between 0 to n(number of element in iterable). default value is n.

Example: Permutation of n out of n items

from itertools import permutations 
MyList = [1, 2, 3]

MyList_permuted = permutations(MyList)
for i in MyList_permuted:
  print(i)

The output of the above code will be:

(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)

Example: Permutation of r items out of n items

from itertools import permutations 
MyList = [1, 2, 3]

MyList_permuted = permutations(MyList, 2)
for i in MyList_permuted:
  print(i)

The output of the above code will be:

(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)

Combination


For combination, import itertools package for combinations method. This method takes iterable as parameter and returns list object containing each combination as a tuple. Iterable must have no repeating elements.

Syntax

combinations(iterable, r)

Parameters

iterable Required. iterable object like list, tuple, set, string , dictionary and range() etc. with no repeating element
r Required. number of items. must be between 0 to n(number of element in iterable).

Example: Combination of r items out of n items

from itertools import combinations 
MyList = [1, 2, 3]

MyList_combination = combinations(MyList, 2)
for i in MyList_combination:
  print(i)

The output of the above code will be:

(1, 2)
(1, 3)
(2, 3)

Combination with replacement


If the replacement of item is allowed, then combinations_with_replacement method is used which is found in itertools package. This method takes iterable as parameter and returns list object containing each combination as a tuple. Iterable must have no repeating elements

Syntax

combinations_with_replacement(iterable, r)

Parameters

iterable Required. iterable object like list, tuple, set, string , dictionary and range() etc. with no repeating element
r Required. number of items. must be between 0 to n(number of element in iterable).

Example: Combination with replacement of r items out of n items

from itertools import combinations_with_replacement 
MyList = [1, 2, 3]

MyList_comb_rplc = combinations_with_replacement(MyList, 2)
for i in MyList_comb_rplc:
  print(i)

The output of the above code will be:

(1, 1)
(1, 2)
(1, 3)
(2, 2)
(2, 3)
(3, 3)