Print all Possible Combinations from the Three Digits - Python
Last Updated :
03 Feb, 2025
The task of printing all possible combinations from three digits in Python involves generating all unique ways to arrange the digits, considering their order. For example, given the digits 1, 2, and 3, the goal is to produce all permutations such as [1, 2, 3], [1, 3, 2], [2, 1, 3], and so on.
itertools.permutations() from itertools module generates all possible ordered arrangements permutations of the given elements. It's efficient and concise, especially when dealing with large datasets, as it eliminates the need for manual looping or recursion.
Python
from itertools import permutations
a = [1, 2, 3]
for combo in permutations(a, 3):
print(combo)
Output(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
Explanation: itertools.permutations generate all 3-element permutations of the list a. The for loop iterates over these permutations and prints each one as a tuple.
Using recursion
Recursion involves a function calling itself to solve smaller instances of the same problem. For generating combinations, the recursive approach systematically builds permutations by fixing one element and recursively permuting the remaining elements.
Python
def fun(arr, path=[]):
# Base case
if len(path) == len(arr):
print(path)
return
# Recursive case
for num in arr:
if num not in path:
fun(arr, path + [num])
a = [1, 2, 3]
# Call the function
fun(a)
Output[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]
Explanation: Base case prints the current permutation stored in path when its length matches that of a , while the recursive case iterates over each element in a, adding it to path if not already present, and recursively calls the function with the updated path.
Using for loop
For loop uses nested loops to manually generate combinations. This approach is straightforward and easy to understand, making it suitable for small datasets, but it becomes less efficient and harder to manage with larger data due to increased complexity.
Python
a = [1, 2, 3]
for i in range(3):
for j in range(3):
for k in range(3):
# Check if the indexes are not the same
if i != j and j != k and i != k:
print(a[i], a[j], a[k])
Output1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
Explanation: This code generates all permutations of the list a using three nested loops, ensuring the indices i, j, and k are distinct before printing the corresponding elements as a permutation.
Similar Reads
Python | All possible N combination tuples Sometimes, while working with Python tuples, we might have a problem in which we need to generate all possible combination pairs till N. This can have application in mathematics domain. Let's discuss certain ways in which this problem can be solved. Method #1 : Using list comprehension + product() T
5 min read
Python - All Possible unique K size combinations till N Sometimes, while working with Python domain, we can have a problem in which we need to produce various combination of elements. This can be K sized unique combinations till N. This problem can have application in data domains and school programming. Let's discuss certain ways in which this task can
4 min read
Python program to find all the Combinations in the list with the given condition Given a list with some elements being a list of optional elements. The task is to find all the possible combinations from all options. Examples: Input: test_list = [1,2,3]Â Output:Â Â [1], [1, 2], [1, 2, 3], [1, 3]Â [2], [2, 3], [3] Example 1: Get all possible combinations of a listâs elements using com
3 min read
Python - Get all numbers combinations in list Sometimes, while working with Python lists, we can have a problem in which we need to concatenate each number with other create new number. This kind of problem is peculiar but can have application in many domains such as day-day programming and gaming. Let's discuss certain ways in which this task
3 min read
Python - Smallest integer possible from combination of list elements Given a list of integers, the task is to get smallest integer possible from the combination of list elements. This is one of the problems that is essential in a competitive point of view and this article discusses various shorthands to solve this problem in Python. Letâs discuss certain ways in whic
4 min read