Python: Generate all sublists of a list
Python List: Exercise - 33 with Solution
Generate All Sublists
Write a Python program to generate all sublists of a list.
Sample Solution:
Python Code:
# Import the 'combinations' function from the 'itertools' module, which is used to generate combinations
from itertools import combinations
# Define a function named 'sub_lists' that generates all possible sublists of a given list 'my_list'
def sub_lists(my_list):
subs = [] # Create an empty list 'subs' to store the sublists
# Iterate through the range of numbers from 0 to the length of 'my_list' + 1
for i in range(0, len(my_list) + 1):
# Use the 'combinations' function to generate all combinations of 'my_list' of length 'i'
temp = [list(x) for x in combinations(my_list, i)]
# Check if 'temp' contains any elements; if so, extend the 'subs' list with the generated sublists
if len(temp) > 0:
subs.extend(temp)
return subs # Return the list of generated sublists
# Define a list 'l1' containing numeric elements and another list 'l2' containing string elements
l1 = [10, 20, 30, 40]
l2 = ['X', 'Y', 'Z']
# Print the original list 'l1'
print("Original list:")
print(l1)
# Print a label indicating the start of sublist generation for 'l1'
print("Sublists of the said list:")
# Call the 'sub_lists' function with 'l1' and print the sublists of 'l1'
print(sub_lists(l1))
# Print a newline for separation
print("\nOriginal list:")
print(l2)
# Print a label indicating the start of sublist generation for 'l2'
print("Sublists of the said list:")
# Call the 'sub_lists' function with 'l2' and print the sublists of 'l2'
print(sub_lists(l2))
Sample Output:
Original list: [10, 20, 30, 40] S [[], [10], [20], [30], [40], [10, 20], [10, 30], [10, 40], [20, 30], [20, 40], [30, 40], [10, 20, 30], [10, 20, 40], [10, 30, 40], [20, 30, 40], [10, 20, 30, 40]] Sublists of the said list: [[], [10], [20], [30], [40], [10, 20], [10, 30], [10, 40], [20, 30], [20, 40], [30, 40], [10, 20, 30], [10, 20, 40], [10, 30, 40], [20, 30, 40], [10, 20, 30, 40]] Original list: ['X', 'Y', 'Z'] Sublists of the said list: [[], ['X'], ['Y'], ['Z'], ['X', 'Y'], ['X', 'Z'], ['Y', 'Z'], ['X', 'Y', 'Z']]
Flowchart:
Python Code Editor:
Previous: Write a Python program to check whether a list contains a sublist.
Next: Write a Python program using Sieve of Eratosthenes method for computing primes upto a specified number.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/python-exercises/list/python-data-type-list-exercise-33.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics