Wednesday, December 25, 2024
Google search engine
HomeLanguagesPython program to find all the Combinations in the list with the...

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

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)

RELATED ARTICLES

Most Popular

Recent Comments