The problem of finding the combinations of list elements of specific size has been discussed. But sometimes, we require more and we wish to have all the combinations of elements of all sizes in range between i and j. Let’s discuss certain ways in which this function can be performed.
Method #1 : Using list comprehension + combinations()
This task can be performed using the list comprehension which can perform the task of varying the combination length and combination() can perform the actual task of finding combinations.
# Python3 code to demonstrate working of # Size Range Combinations in list # Using list comprehension + combinations() from itertools import combinations # initializing list test_list = [4, 5, 6, 7, 3, 8] # printing original list print("The original list is : " + str(test_list)) # initializing i, j i, j = 2, 4 # Size Range Combinations in list # Using list comprehension + combinations() res1 = [com for sub in range(j) for com in combinations(test_list, sub + 1)] res2 = [com for sub in range(i - 1) for com in combinations(test_list, sub + 1)] res = list(set(res1) - set(res2)) # Printing result print("The combinations of elements in range of i and j : " + str(res))
Output :
The original list is : [4, 5, 6, 7, 3, 8] The combinations of elements in range of i and j : [(7, 3), (4, 7), (4, 5, 6, 3), (4, 8), (5, 6), (5, 6, 3), (4, 6, 8), (5, 7, 8), (5, 6, 7, 8), (6, 7, 3), (6, 7, 3, 8), (5, 8), (5, 3, 8), (5, 6, 7), (6, 7), (4, 7, 3, 8), (5, 6, 3, 8), (5, 6, 8), (4, 6, 7, 8), (6, 3), (6, 3, 8), (5, 7, 3, 8), (7, 3, 8), (5, 7, 3), (4, 5, 7, 3), (4, 7, 8), (4, 6, 7), (4, 5, 3), (4, 5), (4, 6, 7, 3), (6, 7, 8), (4, 6, 3, 8), (4, 5, 6, 8), (4, 3, 8), (4, 5, 7, 8), (4, 5, 6), (5, 3), (4, 6, 3), (4, 5, 6, 7), (4, 5, 7), (4, 6), (6, 8), (4, 5, 3, 8), (4, 5, 8), (5, 7), (3, 8), (4, 3), (5, 6, 7, 3), (4, 7, 3), (7, 8)]
Method 2 : Using loop + extend() + combinations()
This method is similar to above method, just the loop is being using to iterate for combination size and extend() performs the task of adding the combinations one after another to final result.
# Python3 code to demonstrate working of # Size Range Combinations in list # Using loop + extend() + combinations() from itertools import combinations # initializing list test_list = [4, 5, 6, 7, 3, 8] # printing original list print("The original list is : " + str(test_list)) # initializing i, j i, j = 2, 4 # Size Range Combinations in list # Using loop + extend() + combinations() res = [] for sub in range(j): if sub >= (i - 1): res.extend(combinations(test_list, sub + 1)) # Printing result print("The combinations of elements in range of i and j : " + str(res))
Output :
The original list is : [4, 5, 6, 7, 3, 8] The combinations of elements in range of i and j : [(7, 3), (4, 7), (4, 5, 6, 3), (4, 8), (5, 6), (5, 6, 3), (4, 6, 8), (5, 7, 8), (5, 6, 7, 8), (6, 7, 3), (6, 7, 3, 8), (5, 8), (5, 3, 8), (5, 6, 7), (6, 7), (4, 7, 3, 8), (5, 6, 3, 8), (5, 6, 8), (4, 6, 7, 8), (6, 3), (6, 3, 8), (5, 7, 3, 8), (7, 3, 8), (5, 7, 3), (4, 5, 7, 3), (4, 7, 8), (4, 6, 7), (4, 5, 3), (4, 5), (4, 6, 7, 3), (6, 7, 8), (4, 6, 3, 8), (4, 5, 6, 8), (4, 3, 8), (4, 5, 7, 8), (4, 5, 6), (5, 3), (4, 6, 3), (4, 5, 6, 7), (4, 5, 7), (4, 6), (6, 8), (4, 5, 3, 8), (4, 5, 8), (5, 7), (3, 8), (4, 3), (5, 6, 7, 3), (4, 7, 3), (7, 8)]
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.