Open In App

Python Program to print all Possible Combinations from the three Digits

Last Updated : 29 Aug, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Given 3 digits a, b, and c. The task is to find all the possible combinations from these digits.

Examples:

Input: [1, 2, 3]
Output:
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

Input: [0, 9, 5]
Output:
0 9 5
0 5 9
9 0 5
9 5 0
5 0 9
5 9 0

Method 1: Brute force or Naive approach

The naive approach is to run 3 loops from 0 to 3 and print all the numbers from the list if the indexes are not equal to each other.

Example:

Python3




# Python program to print all
# the possible combinations
  
def comb(L):
      
    for i in range(3):
        for j in range(3):
            for k in range(3):
                  
                # check if the indexes are not
                # same
                if (i!=j and j!=k and i!=k):
                    print(L[i], L[j], L[k])
                      
# Driver Code
comb([1, 2, 3])


Output:

1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

Method 2: Using itertools.permutations()

This method takes a list as an input and returns an object list of tuples that contain all permutation in a list form.

Example:

Python3




# Python program to print all
# the possible combinations
  
from itertools import permutations
  
# Get all combination of [1, 2, 3]
# of length 3
comb = permutations([1, 2, 3], 3)
  
for i in comb:
    print(i)


Output:

(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)


Similar Reads

itertools.combinations() module in Python to print all possible combinations
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)]Recommended: Please try your approach on {IDE} first, before moving on to the solution. This problem has existing recursive solution please refer Print
2 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 be performed. Input : N = 2, K = 3 Output : [(0, 0
4 min read
Python program to get all unique combinations of two Lists
The combination is a mathematical technique that calculates the number of possible arrangements in a collection of items or list. In combination order of selection doesn't matter. The unique combination of two lists in Python can be formed by pairing each element of the first list with the elements of the second list. Example: List_1 = ["a","b"] Li
5 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 combinations C/C++ Code from itertools import combinat
3 min read
Python program to get all pairwise combinations from a list
Given a list. The task is to write a Python program to get all pairwise combinations from the list. Finding all Pairs (No uniqueness) Example: Input: [1,"Mallika",2,"Yash"] Output: [(1, 'Mallika'), (1, 2), (1, 'Yash'), ('Mallika', 1), ('Mallika', 2), ('Mallika', 'Yash'), (2, 1), (2, 'Mallika'), (2, 'Yash'), ('Yash', 1), ('Yash', 'Mallika'), ('Yash'
4 min read
Python Program to check whether it is possible to make a divisible by 3 number using all digits in an array
Given an array of integers, the task is to find whether it's possible to construct an integer using all the digits of these numbers such that it would be divisible by 3. If it is possible then print "Yes" and if not print "No". Examples: Input : arr[] = {40, 50, 90} Output : Yes We can construct a number which is divisible by 3, for example 945000.
2 min read
Python Program to print all distinct uncommon digits present in two given numbers
Given two positive integers A and B, the task is to print the distinct digits in descending order, which are not common in the two numbers. Examples: Input: A = 378212, B = 78124590Output: 9 5 4 3 0Explanation: All distinct digits present in the two numbers are {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}. The digits {1, 2, 6, 7} are common in both numbers. Inpu
5 min read
Python - All pair combinations of 2 tuples
Sometimes, while working with Python tuples data, we can have a problem in which we need to extract all possible combination of 2 argument tuples. This kind of application can come in Data Science or gaming domains. Let's discuss certain ways in which this task can be performed. Input : test_tuple1 = (7, 2), test_tuple2 = (7, 8) Output : [(7, 7), (
6 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 can be performed. Input : test_list = [7, 3, 4, 5]
3 min read
Python - Find all combinations of overlapping substrings of a string
Given a string, the task is to write a Python program to find all combinations of overlapping substrings of a string and store it in a list. The list of lists will be ordered and grouped by length of substrings. Input : test_str = 'Geeks4G' Output : [['', '', '', '', '', '', '', ''], ['G', 'e', 'e', 'k', 's', '4', 'G'], ['Ge', 'ee', 'ek', 'ks', 's4
2 min read