- Trending Categories
- Data Structure
- Operating System
- C Programming
- 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.
Input : list = [1, 2, 3] Output : , , [1, 2], [1, 2, 3], , [2, 3], ]
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.
# 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))
Enter the size of the First List :: 3 Enter the Element of First List :: 1 2 3 SUBLIST IS ::> [, , [1, 2], [1, 2, 3], , [2, 3], ]
- C# program to print all sublists of a list
- Program to find sum of the sum of all contiguous sublists in Python
- Python Program to print all permutations of a given string
- Python program to print duplicates from a list of integers?
- Program to find number of sublists with sum k in a binary list in Python
- Python Program to Print Middle most Node of a Linked List
- 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
- Count unique sublists within list in Python
- Python program to print all distinct elements of a given integer array.
- Program to find sum of medians of all odd length sublists in C++
- Python Program for Efficient program to print all prime factors of a given number
- Python program to print all even numbers in a range