Python program to find all the Combinations in the list with the given condition
Last Updated :
24 Aug, 2022
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
Python program to get all unique combinations of two Lists The goal is to combine each item from first list with each item from second list in every possible unique way. If we want to get all possible combinations from two lists. Pythonâs itertools library has a function called a product that makes it easy to generate combinations of items from multiple lis
2 min read
Print all Possible Combinations from the Three Digits - Python 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. Using
3 min read
Python program to check if the list contains three consecutive common numbers In Python, we often need to check if a list contains three consecutive numbers that are the same. We can do this by using a simple loop, list slicing or more advanced techniques like regular expressions.Using zip() Functionzip() function in Python is a built-in function that allows us to iterate ove
3 min read
Python program to check if any key has all the given list elements Given a dictionary with list values and a list, the task is to write a Python program to check if any key has all the list elements. Examples: Input : test_dict = {'Gfg' : [5, 3, 1, 6, 4], 'is' : [8, 2, 1, 6, 4], 'best' : [1, 2, 7, 3, 9], 'for' : [5, 2, 7, 8, 4, 1], 'all' : [8, 5, 3, 1, 2]}, find_li
7 min read
Python program to find all possible pairs with given sum Given a list of integers and an integer variable K, write a Python program to find all pairs in the list with given sum K. Examples: Input : lst =[1, 5, 3, 7, 9] K = 12 Output : [(5, 7), (3, 9)] Input : lst = [2, 1, 5, 7, -1, 4] K = 6 Output : [(2, 4), (1, 5), (7, -1)] Method #1: Pythonic Naive This
7 min read