Do you want iteration? itertools.combinations. Common usage:
>>> import itertools
>>> itertools.combinations['abcd',2]
>>> list[itertools.combinations['abcd',2]]
[['a', 'b'], ['a', 'c'], ['a', 'd'], ['b', 'c'], ['b', 'd'], ['c', 'd']]
>>> [''.join[x] for x in itertools.combinations['abcd',2]]
['ab', 'ac', 'ad', 'bc', 'bd', 'cd']
If you just need to compute the formula, math.factorial can be used, but is not fast for large
combinations, but see math.comb
below for an optimized calculation available in Python 3.8+:
import math
def nCr[n,r]:
f = math.factorial
return f[n] // f[r] // f[n-r]
if __name__ == '__main__':
print nCr[4,2]
Output:
6
As of Python 3.8, math.comb
can be used and is much faster:
>>> import math
>>> math.comb[4,2]
6
❮ Math Methods
Example
Find the total number of possibilities to choose k things from n items:
# Import math Library
import math
# Initialize the number of items to choose from
n = 7
# Initialize the number of possibilities to choose
k = 5
# Print total number of possible combinations
print [math.comb[n, k]]
The result will be:
21
Run Example »
Definition and Usage
The math.comb[]
method returns the number of ways picking k unordered outcomes from n possibilities, without repetition, also known as combinations.
Note: The parameters passed in this method must be positive integers.
Syntax
Parameter Values
n | Required. Positive integers of items to choose from |
k | Required. Positive integers of items to choose |
Note: If the value of k is greater than the value of n it will return 0 as a result.
Note: If the parameters are negative, a ValueError occurs. If the parameters are not integers, a TypeError occurs.
Technical Details
An int value, representing the total number of combinations
|
3.8 |
❮ Math Methods
Python provides direct methods to find permutations and combinations of a sequence. These methods are present in itertools package.
Permutation
First import itertools package to implement the permutations method in python. This method takes a list as an input and returns an object list of tuples that contain all permutations in a list form.
Python3
from
itertools
import
permutations
perm
=
permutations[[
1
,
2
,
3
]]
for
i
in
list
[perm]:
print
[i]
Output:
[1, 2, 3] [1, 3, 2] [2, 1, 3] [2, 3, 1] [3, 1, 2] [3, 2, 1]
It generates n! permutations if the length of the input sequence is n.
If want to get permutations of length L then implement it in this way.
Python3
from
itertools
import
permutations
perm
=
permutations[[
1
,
2
,
3
],
2
]
for
i
in
list
[perm]:
print
[i]
Output:
[1, 2] [1, 3] [2, 1] [2, 3] [3, 1] [3, 2]
It generates nCr * r! permutations if the length of the input sequence is n and the input parameter is r.
Combination
This method takes a list and an input r as an input and return an object list of tuples which contain all possible combination of
length r in a list form.
Python3
from
itertools
import
combinations
comb
=
combinations[[
1
,
2
,
3
],
2
]
for
i
in
list
[comb]:
print
[i]
Output:
[1, 2] [1, 3] [2, 3]
1. Combinations are emitted in lexicographic sort order of input. So, if the input list is sorted, the combination tuples will be
produced in sorted order.
Python3
from
itertools
import
combinations
comb
=
combinations[[
1
,
2
,
3
],
2
]
for
i
in
list
[comb]:
print
[i]
Output:
[1, 2] [1, 3] [2, 3]
2. Elements are treated as unique based on their position, not on their value. So if the input elements are unique, there will
be no repeat values in each combination.
Python3
from
itertools
import
combinations
comb
=
combinations[[
2
,
1
,
3
],
2
]
for
i
in
list
[comb]:
print
[i]
Output:
[2, 1] [2, 3] [1, 3]
3. If we want to make a combination of the same element to the same element then we use
combinations_with_replacement.
Python3
from
itertools
import
combinations_with_replacement
comb
=
combinations_with_replacement[[
1
,
2
,
3
],
2
]
for
i
in
list
[comb]:
print
[i]
Output:
[1, 1] [1, 2] [1, 3] [2, 2] [2, 3] [3, 3]