Python – Similar Consecutive elements frequency

Sometimes, while working with Python, we can have a problem in which we have to find the occurrences of elements that are present consecutively. This problem have usage in school programming and data engineering. Let’s discuss certain ways in which this task can be performed.

Method #1 : Using loop
This is brute force method by which this problem can be solved. In this, we iterate loop and count till we get other number.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# Similar Consecutive elements frequency
# using loop
  
# initializing list 
test_list = [2, 2, 3, 3, 3, 3, 4, 4, 4]
  
# printing original list
print ("The original list is : " + str(test_list))
  
# Similar Consecutive elements frequency
# using loop
res = []
count = 1
for ele in range(0, len(test_list) -1):
    if test_list[ele] != test_list[ele + 1]:
        res.append((test_list[ele], count))
        count = 1
    else :
        count = count + 1
res.append((test_list[len(test_list) -1], count))
  
# printing result 
print ("The consecutive element frequency is : " + str(res))

chevron_right


Output :

The original list is : [2, 2, 3, 3, 3, 3, 4, 4, 4]
The consecutive element frequency is : [(2, 2), (3, 4), (4, 3)]

 

Method #2 : Using groupby() + len() + list comprehension
The combination of above methods can be used to perform this task. In this, we group the consecutive elements and extract the count using len(). List comprehension is used to bind both the logics together.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# Similar Consecutive elements frequency
# using groupby() + len() + list comprehension
from itertools import groupby
  
# initializing list 
test_list = [2, 2, 3, 3, 3, 3, 4, 4, 4]
  
# printing original list
print ("The original list is : " + str(test_list))
  
# Similar Consecutive elements frequency
# using groupby() + len() + list comprehension
res = [(k, len(list(j))) for k, j in groupby(test_list)]
  
# printing result 
print ("The consecutive element frequency is : " + str(res))

chevron_right


Output :

The original list is : [2, 2, 3, 3, 3, 3, 4, 4, 4]
The consecutive element frequency is : [(2, 2), (3, 4), (4, 3)]



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.