Python | Combinations of elements till size N in list

27 Aug, 2019
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 till N. 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# Combinations of elements till size N in list# Using list comprehension + combinations()from itertools import combinations  # initializing listtest_list = [4, 5, 6, 7, 3, 8]  # printing original listprint("The original list is : " +  str(test_list))  # Combinations of elements till size N in list# Using list comprehension + combinations()res = [com for sub in range(3) for com in combinations(test_list, sub + 1)]  # Printing resultprint("The combinations of elements till length N : " +  str(res))
The original list is : [4, 5, 6, 7, 3, 8]The combinations of elements till length N : [(4, ), (5, ), (6, ), (7, ), (3, ), (8, ), (4, 5), (4, 6), (4, 7), (4, 3), (4, 8), (5, 6), (5, 7), (5, 3), (5, 8), (6, 7), (6, 3), (6, 8), (7, 3), (7, 8), (3, 8), (4, 5, 6), (4, 5, 7), (4, 5, 3), (4, 5, 8), (4, 6, 7), (4, 6, 3), (4, 6, 8), (4, 7, 3), (4, 7, 8), (4, 3, 8), (5, 6, 7), (5, 6, 3), (5, 6, 8), (5, 7, 3), (5, 7, 8), (5, 3, 8), (6, 7, 3), (6, 7, 8), (6, 3, 8), (7, 3, 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# Combinations of elements till size N in list# Using loop + extend() + combinations()from itertools import combinations  # initializing listtest_list = [4, 5, 6, 7, 3, 8]  # printing original listprint("The original list is : " +  str(test_list))  # Combinations of elements till size N in list# Using loop + extend() + combinations()res = []for sub in range(3):    res.extend(combinations(test_list, sub + 1))  # Printing resultprint("The combinations of elements till length N : " +  str(res))
The original list is : [4, 5, 6, 7, 3, 8]The combinations of elements till length N : [(4, ), (5, ), (6, ), (7, ), (3, ), (8, ), (4, 5), (4, 6), (4, 7), (4, 3), (4, 8), (5, 6), (5, 7), (5, 3), (5, 8), (6, 7), (6, 3), (6, 8), (7, 3), (7, 8), (3, 8), (4, 5, 6), (4, 5, 7), (4, 5, 3), (4, 5, 8), (4, 6, 7), (4, 6, 3), (4, 6, 8), (4, 7, 3), (4, 7, 8), (4, 3, 8), (5, 6, 7), (5, 6, 3), (5, 6, 8), (5, 7, 3), (5, 7, 8), (5, 3, 8), (6, 7, 3), (6, 7, 8), (6, 3, 8), (7, 3, 8)]
Python list-programs
Python
Python Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Python Classes and Objects
Python OOPs Concepts
Introduction To PYTHON
How to drop one or multiple columns in Pandas Dataframe
Defaultdict in Python
Python | Get dictionary keys as a list
Python | Convert a list to dictionary
Python | Convert string dictionary to dictionary
Python Program for Fibonacci numbers