How do you get all the combinations of n numbers in python?

I'm a bit late on this topic, but think I can help someone.

You can use product from itertools:

from itertools import product

n = [1, 2, 3]

result = product(n, repeat=3) # You can change the repeat more then n length

print(list(result))

Output:

[(1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 2, 1), (1, 2, 2), (1, 2, 3), (1, 3, 1),
 (1, 3, 2), (1, 3, 3), (2, 1, 1), (2, 1, 2), (2, 1, 3), (2, 2, 1), (2, 2, 2),
 (2, 2, 3), (2, 3, 1), (2, 3, 2), (2, 3, 3), (3, 1, 1), (3, 1, 2), (3, 1, 3), 
(3, 2, 1), (3, 2, 2), (3, 2, 3), (3, 3, 1), (3, 3, 2), (3, 3, 3)]

Another example, but changing repeat arguement:

from itertools import product

n = [1, 2, 3]

result = product(n, repeat=4) # Changing repeat to 4
print(list(result))

Output:

(1, 1, 2, 3), (1, 1, 3, 1), (1, 1, 3, 2), (1, 1, 3, 3), (1, 2, 1, 1), 
(1, 2, 1, 2), (1, 2, 1, 3), (1, 2, 2, 1), (1, 2, 2, 2), (1, 2, 2, 3), 
(1, 2, 3, 1), (1, 2, 3, 2), (1, 2, 3, 3), (1, 3, 1, 1), (1, 3, 1, 2), 
(1, 3, 1, 3), (1, 3, 2, 1), (1, 3, 2, 2), (1, 3, 2, 3), (1, 3, 3, 1), 
(1, 3, 3, 2), (1, 3, 3, 3), (2, 1, 1, 1), (2, 1, 1, 2), (2, 1, 1, 3), 
(2, 1, 2, 1), (2, 1, 2, 2), (2, 1, 2, 3), (2, 1, 3, 1), (2, 1, 3, 2),
 (2, 1, 3, 3), (2, 2, 1, 1), (2, 2, 1, 2), (2, 2, 1, 3), (2, 2, 2, 1), 
(2, 2, 2, 2), (2, 2, 2, 3), (2, 2, 3, 1), (2, 2, 3, 2), (2, 2, 3, 3), 
(2, 3, 1, 1), (2, 3, 1, 2), (2, 3, 1, 3), (2, 3, 2, 1), (2, 3, 2, 2), 
(2, 3, 2, 3), (2, 3, 3, 1), (2, 3, 3, 2), (2, 3, 3, 3), (3, 1, 1, 1), 
(3, 1, 1, 2), (3, 1, 1, 3), (3, 1, 2, 1), (3, 1, 2, 2), (3, 1, 2, 3), 
(3, 1, 3, 1), (3, 1, 3, 2), (3, 1, 3, 3), (3, 2, 1, 1), (3, 2, 1, 2), 
(3, 2, 1, 3), (3, 2, 2, 1), (3, 2, 2, 2), (3, 2, 2, 3), (3, 2, 3, 1), 
(3, 2, 3, 2), (3, 2, 3, 3), (3, 3, 1, 1), (3, 3, 1, 2), (3, 3, 1, 3), 
(3, 3, 2, 1), (3, 3, 2, 2), (3, 3, 2, 3), (3, 3, 3, 1), (3, 3, 3, 2), 
(3, 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.

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) 

How do you get all the combinations of a set of numbers in Python?

To find all the combinations of a Python list, also known as a powerset, follow these steps:.
Import the built-in itertools module..
Specify a list of items..
Initialize an empty list for storing the combinations..
Create a loop that loops values from 0 to the length of the list + 1..

How do you find all possible combinations of n numbers?

C Program to Generate All Possible Combinations of a Given List of Numbers.
#include<stdio.h>.
#include<string.h>.
#define N 10..
void print(int *num, int n).
int i;.
for ( i = 0 ; i < n ; i++).
printf("%d ", num[i]);.

How do you generate all possible combinations of one list?

To list all combinations possible in an Excel sheet, follow the following procedure;.
Step 1: Open the sheet. You first need to open the sheet with data from which you want to make all possible combinations. ... .
Step 2: Select cell for result. ... .
Step 3: Drag the formula to other cells..

How do you generate all possible combinations of two lists in Python?

How to get all unique combinations of two lists in Python.
list1 = ["a", "b", "c"].
list2 = [1, 2].
all_combinations = [].
list1_permutations = itertools. permutations(list1, len(list2)) ... .
for each_permutation in list1_permutations:.
zipped = zip(each_permutation, list2).
all_combinations. ... .
print(all_combinations).