Python program to find combinations

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) 

Given an array of size n, generate and print all possible combinations of r elements in array.

Examples:

Input : arr[] = [1, 2, 3, 4],  
            r = 2
Output : [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]

This problem has existing recursive solution please refer Print all possible combinations of r elements in a given array of size n link. We will solve this problem in python using itertools.combinations() module.

What does itertools.combinations() do ?

It returns r length subsequences of elements from the input iterable. Combinations are emitted in lexicographic sort order. So, if the input iterable is sorted, the combination tuples will be produced in sorted order.

  • itertools.combinations(iterable, r) :
    It return r-length tuples in sorted order with no repeated elements. For Example, combinations(‘ABCD’, 2) ==> [AB, AC, AD, BC, BD, CD].
  • itertools.combinations_with_replacement(iterable, r) :
    It return r-length tuples in sorted order with repeated elements. For Example, combinations_with_replacement(‘ABCD’, 2) ==> [AA, AB, AC, AD, BB, BC, BD, CC, CD, DD].
  • from itertools import combinations

    def rSubset(arr, r):

        return list(combinations(arr, r))

    if __name__ == "__main__":

        arr = [1, 2, 3, 4]

        r = 2

        print (rSubset(arr, r))

    Output:

    [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]
    

    This article is contributed by Shashank Mishra (Gullu). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

How do you find the number of combinations in Python?

Python: Find the number of combinations of a,b,c and d.
Input: ... .
Sample Solution:.
Python Code: import itertools print("Input the number(n):") n=int(input()) result=0 for (i,j,k) in itertools.product(range(10),range(10),range(10)): result+=(0<=n-(i+j+k)<=9) print("Number of combinations:",result) ... .
Flowchart:.

What is combinations in Python?

Combination is a collection of the element where the order doesn't matter. Python itertools module provides the combination() method to calculate the combination of given data. We can calculate the combination of a string.

How do you get different combinations of a string in Python?

In Python, we can use the built-in module itertools to get permutations of elements in the list using the permutations() function. However, we can also write your utility function to generate all permutations of a string. We can do this either recursively or iteratively.

How do you find all the combinations of numbers?

To calculate combinations, we will use the formula nCr = n! / r! * (n - r)!, where n represents the total number of items, and r represents the number of items being chosen at a time. To calculate a combination, you will need to calculate a factorial.