Python – Count frequency of Sublist in given list

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output

The original list is : [4, 5, 3, 5, 7, 8, 3, 5, 7, 2, 7, 3, 2]
The sublist count : 2

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output

The original list is : [4, 5, 3, 5, 7, 8, 3, 5, 7, 2, 7, 3, 2]
The sublist count : 2



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.