Given a list, print all the sublists of a list. Python provides efficient and elegant ways to generate and manipulate sublists. In this article, we will explore different methods how to print all sublists of a given list.
Examples:
Input: list = [1, 2, 3]
Output: [[], [1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
Input: [1, 2, 3, 4]
Output: [[], [1], [1, 2], [1, 2, 3], [1, 2, 3, 4], [2], [2, 3], [2, 3, 4], [3], [3, 4], [4]]
Explanation: In This, we are printing all sublists of a list in Python.
Python program to print all sublists of a list
There are multiple approaches to generating sublists in Python. Let’s explore a few methods and their corresponding code snippets:
- Using Nested Loops
- Using List Comprehension
- Using Iterative Approach
- Using Recursion
Get all possible sublists of a list using Nested Loops
To generate and print all sublists of a list using nested loops, you need to iterate through the list with two loops: an outer loop and an inner loop. The outer loop sets the starting index of the sublist, while the inner loop determines the ending index. By controlling these indices, you can extract and print all the possible sublists.
Python3
def sublists(lst):
n = len (lst)
sublists = []
for start in range (n):
for end in range (start + 1 , n + 1 ):
sublists.append(lst[start:end])
return sublists
original_list = [ 1 , 2 , 3 ]
sublists_nested = sublists(original_list)
print (sublists_nested)
|
Output:
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
Get all possible sublists of a list using List Comprehension
Using list comprehension to create sublists of a list is a clear and effective technique. You can create new lists using list comprehension by defining a short expression that iteratively evaluates each item in the list. List comprehension can effectively achieve the required result when used to create sublists.
Python3
def sublists(lst):
return [lst[i:j] for i in range ( len (lst)) for j in range (i + 1 , len (lst) + 1 )]
original_list = [ 9 , 8 , 7 ]
sublists_list = sublists(original_list)
print (sublists_list)
|
Output:
[[9], [9, 8], [9, 8, 7], [8], [8, 7], [7]]
Get all possible sublists of a list using Iterative Method
In this, we uses an iterative approach to print all possible sublists of a given list. It iterates through the list, using two nested loops to determine the starting and ending indices of each sublist. The sublists are then printed one by one. Below is the Python implementation of the above approach:
Python
def sublists(lst):
sublists_list = []
for i in range ( len (lst)):
for j in range (i + 1 , len (lst) + 1 ):
sublists_list.append(lst[i:j])
return sublists_list
original_list = [ 2 , 3 , 4 , 9 ]
sublists_list = sublists(original_list)
print (sublists_list)
|
Output:
[[2], [2, 3], [2, 3, 4], [2, 3, 4, 9], [3], [3, 4], [3, 4, 9], [4], [4, 9], [9]]
Time Complexity: O(n*n)
Auxiliary Space: O(n)
Generate all sublists of a list using Recursion
In this, we defines a recursive function generate_sublists
that generates all sublists of a given list by including or excluding the first element. It builds sublists by recursively generating sublists for the rest of the elements and combining them with the first element. The result is a list containing all possible sublists of the input list.
Python3
def sublists(lst, index = 0 , current = []):
if index = = len (lst):
print (current)
return
sublists(lst, index + 1 , current)
sublists(lst, index + 1 , current + [lst[index]])
lst = [ 1 , 2 , 3 ]
sublists(lst)
|
Output:
[]
[3]
[2]
[2, 3]
[1]
[1, 3]
[1, 2]
[1, 2, 3]
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
18 Aug, 2023
Like Article
Save Article