How do you make a number combination 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)]```

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 the combination of a number in Python?

Use the itertools.combinations() Function to Find the Combinations of a List in Python..
Use the itertools.combinations_with_replacement() Function to Find the Combinations of a List in Python..
Create a User-Defined powerset() Function to Find the Combinations of a List in Python..

How do you implement a combination in Python?

To create combinations without using itertools, iterate the list one by one and fix the first element of the list and make combinations with the remaining list. Similarly, iterate with all the list elements one by one by recursion of the remaining list.

How do you do number combinations?

In fact, if you know the number of combinations, you can easily calculate the number of permutations: P(n,r) = C(n,r) * r! . If you switch on the advanced mode of this combination calculator, you will be able to find the number of permutations.

How does combinations work in Python?

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.