- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python program to print all sublists of a list.
Given a list, print all the sublists of a list.
Examples −
Input : list = [1, 2, 3] Output : [], [1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
Algorithm
Step 1 : given a list. Step 2 : take one sublist which is empty initially. Step 3 : use one for loop till length of the given list. Step 4 : Run a loop from i+1 to length of the list to get all the sub arrays from i to its right. Step 5 : Slice the sub array from i to j. Step 6 : Append it to an another list to store it. Step 7 : Print it at the end.
Example Code
# Python program to print all # sublist from a given list # function to generate all the sub lists def displaysublist(A): # store all the sublists B = [[ ]] # first loop for i in range(len(A) + 1): # second loop for j in range(i + 1, len(A) + 1): # slice the subarray sub = A[i:j] B.append(sub) return B # driver code A=list() n=int(input("Enter the size of the First List ::")) print("Enter the Element of First List ::") for i in range(int(n)): k=int(input("")) A.append(k) print("SUBLIST IS ::>",displaysublist(A))
Output
Enter the size of the First List :: 3 Enter the Element of First List :: 1 2 3 SUBLIST IS ::> [[], [1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
- Related Articles
- C# program to print all sublists of a list
- Program to find sum of the sum of all contiguous sublists in Python
- Program to find number of sublists with sum k in a binary list in Python
- Python program to print duplicates from a list of integers?
- Python Program to print all permutations of a given string
- Python Program to print unique values from a list
- Python program to print even numbers in a list
- Python program to print negative numbers in a list
- Python program to print odd numbers in a list
- Python Program to Print Middle most Node of a Linked List
- Count unique sublists within list in Python
- Python program to print all even numbers in a range
- Python program to print all odd numbers in a range
- Python program to print all distinct elements of a given integer array.
- Python Program for Efficient program to print all prime factors of a given number

Advertisements