 # Print all sublists of a list in Python

Given a list, print all the sublists of a list.
Examples:

```Input  : list = [1, 2, 3]
Output : [[], , [1, 2], [1, 2, 3], ,
[2, 3], ]

Input : [1, 2, 3, 4]
Output : [[], , [1, 2], [1, 2, 3], [1, 2, 3, 4],
, [2, 3], [2, 3, 4], , [3, 4], ]```

Approach:

The approach will be run two nested loops till the length of the given list. The outer loop i traverse from 0 to the length of the list and the inner loop goes from 0 to i. Need to add 1 to length because the range only goes from 0 to i-1. To get the subarray we can use slicing to get the subarray.

Step 1: Run a loop till length+1 of the given list.
Step 2: Run another loop from 0 to i.
Step 3: Slice the subarray from j to i.
Step 4: Append it to a another list to store it
Step 5: Print it at the end

Below is the Python implementation of the above approach:

 `# Python program to print all` `# sublist from a given list` `# function to generate all the sub lists` `def` `sub_lists (l):` `    ``lists ``=` `[[]]` `    ``for` `i ``in` `range``(``len``(l) ``+` `1``):` `        ``for` `j ``in` `range``(i):` `            ``lists.append(l[j: i])` `    ``return` `lists` `# driver code` `l1 ``=` `[``1``, ``2``, ``3``]` `print``(sub_lists(l1))`

Output:

`[[], , , [1, 2], , [2, 3], [1, 2, 3]]`

Last Updated on November 13, 2021 by admin

## Python Dictionary update() methodPython Dictionary update() method

Python Dictionary update() method Python Dictionary update() method updates the dictionary with the elements from another dictionary object or from an iterable of key/value pairs. Syntax: dict.update([other])   Parameters: This method takes either a dictionary or an iterable object of key/value pairs (generally

## Python datetime modulePython datetime module

Python datetime module In Python, date and time are not a data type of their own, but a module named datetime can be imported to work with the date as well as time. Python Datetime module comes built into Python, so there is no

## range() to a list in Pythonrange() to a list in Python

Python range() to a list   Often times we want to create a list containing a continuous value like, in a range of 100-200. Let’s discuss how to create a list using the range() function. Will this work ? #

Downloading files from web using Python Requests is a versatile HTTP library in python with various applications. One of its applications is to download a file from web using the file URL. Installation: First of all, you would need to download the

## Python LoopsPython Loops

loops in python Python programming language provides following types of loops to handle looping requirements. Python provides three ways for executing the loops. While all the ways provide similar basic functionality, they differ in their syntax and condition checking time.

## Pandas DataFrame reset_index() methodPandas DataFrame reset_index() method

Pandas DataFrame reset_index() Method with Exmaples Pandas reset_index() method is a powerful tool to reset the index of a DataFrame. This method sets a list of integers ranging from 0 to the length of the data as the index. In

## Pandas DataFrame.set_index()Pandas DataFrame.set_index()

Python Pandas DataFrame.set_index()   Pandas set_index() is a method to set a List, Series or Data frame as index of a Data Frame. Index column can be set while making a data frame too. But sometimes a data frame is

## Generate all permutation of a set in PythonGenerate all permutation of a set in Python

Generate all permutation of a set in Python Permutation is an arrangement of objects in a specific order. Order of arrangement of object is very important. The number of permutations on a set of n elements is given by  n!.  For example,