How to get all sublists of a list in Python

Get all sublists of a list in Python in two ways:

In this post, we will learn how to print all sublists from a list in Python. The input of the program is a list and the output is a list holding all sublists. We will write one program that will take the list inputs one by one and print out the final output.

So, our program will work as below:

  • Take the total size of the list from the user.
  • Read the values one by one from the user.
  • Print out the final list of sublists.

Method 1: Get all sublists of a Python list with nested for loops:

We will use one nested for loop to find out all combinations of a list.

  • Run one loop in the range of 0 to length of the list.
  • Run one inner loop in the range of current outer loop to length of the list.
  • Get the slice of the list in between the current indices pointed by the outer loop and inner loop.
  • Add the sliced list to the final list.
  • Return the final list.

Python program:

Below is the complete Python program:

given_list = list()
result_list = list()

size = int(input('Enter the size of the list :'))

print('Enter all elements of the list :')

for i in range(size):
    given_list.append(int(input('Enter element to add : ')))

for i in range(len(given_list) + 1):
    for j in range(i + 1, len(given_list) + 1):
        result_list.append(given_list[i:j])

print(given_list)
print(result_list)

Download it on Github

Here,

  • given_list is the original list entered by the user.
  • result_list is the final list i.e. lists of lists.
  • The size variable holds the size of the list. We are reading this value from the user.
  • The first for loop is used to read all values for the list one by one. We are using the input() method to read the values. Each value is converted to an integer with the int() method and appends to the list given_list.
  • The last nested for loops are used to create the final list. It uses list slicing to get the sublists.
  • At the end of the program, we are printing the original and the final lists.

Output:

This program will print the below output:

Enter the size of the list :3
Enter all elements of the list :
Enter element to add : 1
Enter element to add : 2
Enter element to add : 3
[1, 2, 3]
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]


Enter the size of the list :5
Enter all elements of the list :
Enter element to add : 1
Enter element to add : 2
Enter element to add : 3
Enter element to add : 4
Enter element to add : 5
[1, 2, 3, 4, 5]
[[1], [1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 5], [2], [2, 3], [2, 3, 4], [2, 3, 4, 5], [3], [3, 4], [3, 4, 5], [4], [4, 5], [5]]

Method 2: Find the sublists of a list recursively:

This problem can also be solved recursively. With this approach, it will find the sublists of all the elements excluding the first element of the list recursively, append the elements with the first element and adds all to the final list.

def get_sublists(l):
    if len(l) == 0:
        return [[]]

    sublists = []
    first = l[0]
    remaining = l[1:]
    sublist_remaining = get_sublists(remaining)

    for sublist in sublist_remaining:
        sublists.append([first] + sublist)

    sublists.extend(sublist_remaining)

    return sublists


if __name__ == "__main__":
    given_list = list()
    result_list = list()

    size = int(input("Enter the size of the list: "))

    print("Enter all elements of the list: ")

    for i in range(size):
        given_list.append(int(input("Enter element to add: ")))

    result_list = get_sublists(given_list)

    print(result_list)

Download it on Github

Python get sublists from a list

Sample output:

Enter the size of the list: 4
Enter all elements of the list:
Enter element to add: 1
Enter element to add: 2
Enter element to add: 3
Enter element to add: 4
[[1, 2, 3, 4], [1, 2, 3], [1, 2, 4], [1, 2], [1, 3, 4], [1, 3], [1, 4], [1], [2, 3, 4], [2, 3], [2, 4], [2], [3, 4], [3], [4], []]

You might also like: