List all combinations of 4 numbers 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)]```

To find the combinations of length r in a Python list, use itertools.combinations() method:

import itertools

combinations = itertools.combinations(list_items, r)

To find all the combinations of a Python list, also known as a powerset, follow these steps:

  1. Import the built-in itertools module.
  2. Specify a list of items.
  3. Initialize an empty list for storing the combinations.
  4. Create a loop that loops values from 0 to the length of the list + 1.
  5. Create an inner loop that adds combinations of length r to the list of combinations.

Here is how it looks in the code:

import itertools

numbers = [1, 2, 3]

combinations = []

for r in range(len(numbers)+1):
    for combination in itertools.combinations(numbers, r):
        combinations.append(combination)

print(combinations)

Output:

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

How to Get All Combinations of Unique Elements in Python

So you want no duplicate values in the list of all the combinations? No problem.

To obtain the unique powerset in Python, convert the list to a set to remove duplicates. Otherwise, use the same approach as above.

Here is how the code looks after the change:

import itertools

numbers = [1, 3, 3]

combinations = []

for r in range(len(numbers)+1):
    for combination in itertools.combinations(set(numbers), r):
        combinations.append(combination)

print(combinations)

Now it only returns the combinations where there are no duplicate values:

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

Powerset Recipes—A Simpler and Faster Approach

As per itertools docs, you can use a slightly simpler and perhaps more efficient approach to get all the combinations of an iterable.

Here is the code:

from itertools import chain, combinations

def powerset(items):
    l_items = list(items)
    return chain.from_iterable(combinations(l_items, r) for r in range(len(l_items) + 1))

numbers = [1, 2, 3]

print(list(powerset(numbers)))

Output:

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

Powerset Recipes—Find All Combinations of Unique Elements

To find all the combinations of unique values, you need to make a tiny change to the above powerset() function. Namely, convert the iterable items to a set to remove the duplicates.

Here is how the code looks now with an example call:

from itertools import chain, combinations

def powerset(items):
    l_items = list(set(items))
    return chain.from_iterable(combinations(l_items, r) for r in range(len(l_items) + 1))

numbers = [3, 1, 3]

print(list(powerset(numbers)))

Output:

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

Conclusion

Today you learned how to obtain all the combinations of a Python list. This group of sets is also known as the powerset of a list.

Thanks for reading. I hope you find it useful.

Happy coding!

Further Reading

Python Tricks and Tips

Python Interview Questions

Python Advanced Features

How do you find the combinations of 4 numbers in Python?

“python all possible combinations of 4 numbers” Code Answer's.
a=int(input("Enter first number:")).
b=int(input("Enter second number:")).
c=int(input("Enter third number:")).
d. append(a).
d. append(b).
d. append(c).
for i in range(0,3):.

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

Powerset—How to Get All Combinations of a List in Python.
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..

What are all possible combinations of 4 numbers?

There are 10,000 possible combinations that the digits 0-9 can be arranged into to form a four-digit code.

How do you get a list of combinations in Python?

Combinations of a List 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..