
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python – Count frequency of sublist in given list
When it is required to count the frequency of a sub-list in a given list, a list comprehension and the ‘len’ method along with the ‘if’ condition are used.
Example
Below is a demonstration of the same −
my_list = [23, 33, 45, 67, 54 , 43, 33, 45, 67, 83, 33, 45, 67,90, 0] print("The list is : " ) print(my_list) sub_list = [33, 45, 67, 90] print("The sub-list is : " ) print(sub_list) my_result = len([sub_list for index in range(len(my_list)) if my_list[index : index + len(sub_list)] == sub_list]) print("The resultant list is : ") print(my_result)
Output
The list is : [23, 33, 45, 67, 54, 43, 33, 45, 67, 83, 33, 45, 67, 90, 0] The sub-list is : [33, 45, 67, 90] The resultant list is : 1
Explanation
A list of integers is defined and is displayed on the console.
The sub-list is defined and is displayed on the console.
A list comprehension is used to iterate over the list, and use an ‘if’ condition.
This condition checks if the sum of the length of the sub-list and the specific index and the sublist are equal.
If yes, they are appended to a list, and assigned to a variable.
This is displayed as output on the console.
- Related Articles
- Find the sublist with maximum value in given nested list in Python
- Find sum of frequency of given elements in the list in Python
- Count all prefixes in given string with greatest frequency using Python
- List frequency of elements in Python
- Find frequency of given character at every position in list of lists in Python
- Count Frequency of Highest Frequent Elements in Python
- Program to check sublist sum is strictly greater than the total sum of given list Python
- Python – Fractional Frequency of elements in List
- Finding frequency in list of tuples in Python
- Count the sublists containing given element in a list in Python
- Count minimum frequency elements in a linked list in C++
- Program to count minimum k length sublist that can be flipped to make all items of list to 0 in Python
- Python – Restrict Elements Frequency in List
- Python program to count distinct words and count frequency of them
- Program to find length of longest sublist with given condition in Python

Advertisements