In this program, we will take a user input as a list. Generate all sublists having 5 or more elements in it from the given list with the help of the below-given steps.
Generate all sublists of a list:
Steps to solve the program
- Take a list as input and create an empty list to store and generate all sublists.
- From itertools import combinations for generating sublists with 5 or more elements.
- Use for loop and range function for this purpose.
- If the generated list contains 5 or more elements add that list as a sublist to the empty list.
- Print the list to see the output.
#Importing combinations
from itertools import combinations
#Input list
list1 = [1,3,5,8,0,22,45]
#Creating an empty list
sub = []
for i in range(0,len(list1)+1):
#Checking for combinations
for sublist in combinations(list1,i):
temp = list(sublist)
if len(temp) > 4:
sub.append(temp)
#Printing output
print(sub)
Output :
[[1, 3, 5, 8, 0],
[1, 3, 5, 8, 22],
[1, 3, 5, 8, 45],
[1, 3, 5, 0, 22],
[1, 3, 5, 0, 45],
[1, 3, 5, 22, 45],
[1, 3, 8, 0, 22],
[1, 3, 8, 0, 45],
[1, 3, 8, 22, 45],
[1, 3, 0, 22, 45],
[1, 5, 8, 0, 22],
[1, 5, 8, 0, 45],
[1, 5, 8, 22, 45],
[1, 5, 0, 22, 45],
[1, 8, 0, 22, 45],
[3, 5, 8, 0, 22],
[3, 5, 8, 0, 45],
[3, 5, 8, 22, 45],
[3, 5, 0, 22, 45],
[3, 8, 0, 22, 45],
[5, 8, 0, 22, 45],
[1, 3, 5, 8, 0, 22],
[1, 3, 5, 8, 0, 45],
[1, 3, 5, 8, 22, 45],
[1, 3, 5, 0, 22, 45],
[1, 3, 8, 0, 22, 45],
[1, 5, 8, 0, 22, 45],
[3, 5, 8, 0, 22, 45],
[1, 3, 5, 8, 0, 22, 45]]
Related Articles
Python program to find the second largest number from the list.
Python program to find the second smallest number from the list.
Python program to merge all elements of the list in a single entity using a special character.
Python program to get the difference between two lists.
Python program to reverse each element of the list.
Python program to combine two list elements as a sublist in a list.