Python - Count frequency of Sublist in given list
Last Updated :
10 Mar, 2023
Given a List and a sublist, count occurrence of sublist in list.
Input : test_list = [4, 5, 3, 5, 7, 8, 3, 5, 7, 2, 3, 5, 7], sublist = [3, 5, 7] Output : 3 Explanation : 3, 5, 7 occurs 3 times. Input : test_list = [4, 5, 3, 5, 8, 8, 3, 2, 7, 2, 3, 6, 7], sublist = [3, 5, 7] Output : 0 Explanation : No occurrence found.
Method #1 : Using list comprehension + slicing
In this, we test for each sublist of list extracted using slicing, if found, the element is added to list, at last length of list is computed using len().
Python3
# Python3 code to demonstrate working of
# Sublist Frequency
# Using list comprehension + slicing
# initializing list
test_list = [4, 5, 3, 5, 7, 8, 3, 5, 7, 2, 7, 3, 2]
# printing original list
print("The original list is : " + str(test_list))
# initializing Sublist
sublist = [3, 5, 7]
# slicing is used to extract chunks and compare
res = len([sublist for idx in range(len(test_list)) if test_list[idx : idx + len(sublist)] == sublist])
# printing result
print("The sublist count : " + str(res))
OutputThe original list is : [4, 5, 3, 5, 7, 8, 3, 5, 7, 2, 7, 3, 2]
The sublist count : 2
Time Complexity: O(n), where n is the length of the input list.
Auxiliary Space: O(1) additional space is not required
Method #2 : Using zip_longest() + islice() + all() + loop
In this, we perform task of slicing using islice() and check if each element is matching using all(), zip_longest() helps in mapping elements to check for equality from sublist. Loop is used to get to index match with first element of sublist in list, to make it more efficient.
Python3
# Python3 code to demonstrate working of
# Sublist Frequency
# Using zip_longest() + islice() + all() + loop
from itertools import zip_longest, islice
# initializing list
test_list = [4, 5, 3, 5, 7, 8, 3, 5, 7, 2, 7, 3, 2]
# printing original list
print("The original list is : " + str(test_list))
# initializing Sublist
sublist = [3, 5, 7]
# slicing is used to extract chunks and compare
res = []
idx = 0
while True:
try:
# getting to the index
idx = test_list.index(sublist[0], idx)
except ValueError:
break
# using all() to check for all elements equivalence
if all(x == y for (x, y) in zip_longest(sublist, islice(test_list, idx, idx + len(sublist)))):
res.append(sublist)
idx += len(sublist)
idx += 1
res = len(res)
# printing result
print("The sublist count : " + str(res))
OutputThe original list is : [4, 5, 3, 5, 7, 8, 3, 5, 7, 2, 7, 3, 2]
The sublist count : 2
Similar Reads
Python | Find sum of frequency of given elements in the list Given two lists containing integers, the task is to find the sum of the frequency of elements of the first list in the second list. Example: Input: list1 = [1, 2, 3] list2 = [2, 1, 2, 1, 3, 5, 2, 3] Output: 7 Explanation: No of time 1 occurring in list2 is :2 No of time 2 occurring in list2 is :3 No
4 min read
Python - Step Frequency of elements in List Sometimes, while working with Python, we can have a problem in which we need to compute frequency in list. This is quite common problem and can have usecase in many domains. But we can atimes have problem in which we need incremental count of elements in list. Let's discuss certain ways in which thi
4 min read
Python | Finding frequency in list of tuples In python we need to handle various forms of data and one among them is list of tuples in which we may have to perform any kind of operation. This particular article discusses the ways of finding the frequency of the 1st element in list of tuple which can be extended to any index. Let's discuss cert
6 min read
Python | Frequency of substring in given string Finding a substring in a string has been dealt with in many ways. But sometimes, we are just interested to know how many times a particular substring occurs in a string. Let's discuss certain ways in which this task is performed. Method #1: Using count() This is a quite straightforward method in whi
6 min read
Count the Sublists Containing given Element in a List - Python Given a list of lists our task is to count the number of sublists containing the given element x. For example: li = [[1, 3, 5], [1, 3, 5, 7], [1, 3, 5, 7, 9]] and x = 1 then output will be 3.Using List ComprehensionThis method uses list comprehension to check for the presence of the given element in
2 min read
Python | Frequency grouping of list elements Sometimes, while working with lists, we can have a problem in which we need to group element along with it's frequency in form of list of tuple. Let's discuss certain ways in which this task can be performed. Method #1: Using loop This is a brute force method to perform this particular task. In this
6 min read