Open In App

Python program to find all the Combinations in the list with the given condition

Last Updated : 24 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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

Python3




from itertools import combinations
# initializing list
test_list = ["GFG", [5, 4], "is",
            ["best", "good", "better", "average"]]
idx=0
temp = combinations(test_list, 2)
for i in list(temp):
    idx = idx+1
    print ("Combination", idx, ": ", i)


Output:

Combination 1 :  ('GFG', [5, 4])
Combination 2 :  ('GFG', 'is')
Combination 3 :  ('GFG', ['best', 'good', 'better', 'average'])
Combination 4 :  ([5, 4], 'is')
Combination 5 :  ([5, 4], ['best', 'good', 'better', 'average'])
Combination 6 :  ('is', ['best', 'good', 'better', 'average'])

Example 2: Get all possible combinations of a list’s elements using combinations_with_replacement

Python3




from itertools import combinations_with_replacement
 
# initializing list
test_list = ["GFG", [5, 4], "is",
            ["best", "good", "better", "average"]]
idx=0
temp = combinations_with_replacement(test_list, 2)
for i in list(temp):
    idx = idx+1
    print ("Combination", idx, ": ", i)


Output:

Combination 1 :  ('GFG', 'GFG')
Combination 2 :  ('GFG', [5, 4])
Combination 3 :  ('GFG', 'is')
Combination 4 :  ('GFG', ['best', 'good', 'better', 'average'])
Combination 5 :  ([5, 4], [5, 4])
Combination 6 :  ([5, 4], 'is')
Combination 7 :  ([5, 4], ['best', 'good', 'better', 'average'])
Combination 8 :  ('is', 'is')
Combination 9 :  ('is', ['best', 'good', 'better', 'average'])
Combination 10 :  (['best', 'good', 'better', 'average'], ['best', 'good', 'better', 'average'])

Example 3: Get all possible combinations of a list’s elements using loop

In this, we use a nested loop to get index wise combinations from each nested option list, and then the outer loop is used to get default values in all combinations.

Python3




def combinations(iterable, r):
    pool = tuple(iterable)
    n = len(pool)
    if r > n:
        return
    indx = list(range(r))
    yield tuple(pool[i] for i in indx)
    while True:
        for i in reversed(range(r)):
            if indx[i] != i + n - r:
                break
        else:
            return
        indx[i] += 1
        for j in range(i+1, r):
            indx[j] = indx[j-1] + 1
        yield tuple(pool[i] for i in indx)
 
 
x = [2, 3, 1, 6, 4, 7]
for i in combinations(x, 2):
    print(i)


Output:

(2, 3)
(2, 1)
(2, 6)
(2, 4)
(2, 7)
(3, 1)
(3, 6)
(3, 4)
(3, 7)
(1, 6)
(1, 4)
(1, 7)
(6, 4)
(6, 7)
(4, 7)

Example 4: Get all possible combinations of a list’s elements using recursion

Python3




import copy
 
def combinations(target, data):
 
    for i in range(len(data)):
 
        new_lis = copy.copy(target)
        new_data = copy.copy(data)
#         print(new_lis, new_data)
        new_lis.append(data[i])
        new_data = data[i+1:]
 
        print(new_lis)
 
        combinations(new_lis,
                     new_data)
 
 
target = []
data = [1, 2, 3, 4]
 
combinations(target, data)


Output:

(2, 3)
(2, 1)
(2, 6)
(2, 4)
(2, 7)
(3, 1)
(3, 6)
(3, 4)
(3, 7)
(1, 6)
(1, 4)
(1, 7)
(6, 4)
(6, 7)
(4, 7)


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 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 - 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 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 print all Possible Combinations from the three Digits
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 t
2 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
Python | Find Mixed Combinations of string and list
Sometimes, while working with Python, we can have a problem in which we need to make combinations of string and character list. This type of problem can come in domains in which we need to interleave the data. Let's discuss certain ways in which this task can be performed. Method #1 : Using loop + enumerate() + replace() This task can be performed
6 min read
All Combinations For A List Of Objects
Prerequisite: Python Itertools There are several ways to get all combinations for a list of objects in python. This problem has already a recursive solution. Python has an itertools module that provides two functions named combinations() and combinations_with_replacement() which make our work a lot easier. Below are the two methods: 1. Using iterto
3 min read
Python | Check if all elements in list follow a condition
Sometimes, while working with Python list, we can have a problem in which we need to check if all the elements in list abide to a particular condition. This can have application in filtering in web development domain. Let's discuss certain ways in which this task can be performed. Method #1 : Using all() We can use all(), to perform this particular
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 be performed. Input : N = 2, K = 3 Output : [(0, 0
4 min read
Practice Tags :