Open In App

Python – Similar Consecutive elements frequency

Last Updated : 02 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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. 

Python3




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


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

Time Complexity: O(n)
Auxiliary Space: O(n)

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. 

Python3




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


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 #3 : Using ordereddict

Python3




from collections import OrderedDict
 
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 OrderedDict
result = []
temp_dict = OrderedDict()
for ele in test_list:
    if ele not in temp_dict:
        temp_dict[ele] = 1
    else:
        temp_dict[ele] += 1
 
for key, value in temp_dict.items():
    result.append((key, value))
 
# printing result
print ("The consecutive element frequency is : " + str(result))


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

This approach has a time complexity of O(n) and space complexity of O(n). The OrderedDict data structure is used to store the elements and their frequencies in a dictionary, while preserving the order of insertion. This approach iterates through the given list and keeps track of the frequency of each element in the temp_dict dictionary. Finally, the resulting frequency of each element is appended to the result list.

Method #4: Using itertools.groupby()

  • We first import the itertools module which provides functions for working with iterators.
  • We then initialize the list test_list.
  • Next, we use the groupby() function from itertools to group consecutive elements in the list based on their value.
  • The groupby() function returns an iterator that produces pairs (key, group), where key is the value that is common to all elements in the group and group is an iterator that produces all the consecutive elements with that value.
  • We use a list comprehension to convert the iterator of groups to a list of tuples (k, len(list(g))) where k is the key and len(list(g)) is the length of the group.
  • Finally, we print the result

Python3




import itertools
 
# 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 itertools.groupby()
res = [(k, len(list(g))) for k, g in itertools.groupby(test_list)]
 
# printing result
print("The consecutive element frequency is : " + str(res))


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

Time complexity: O(N) where N is the length of the input list since we iterate over the list only once.
Auxiliary space: O(N) since we need to store the result in a list of tuples with at most N elements.

Method #5: Using a dictionary. 

Python3




# Python3 code to demonstrate
# Similar Consecutive elements frequency
# using dictionary
 
# 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 dictionary
freq_dict = {}
prev = None
for num in test_list:
    if num == prev:
        freq_dict[num] += 1
    else:
        freq_dict[num] = 1
    prev = num
res = [(k, v) for k, v in freq_dict.items()]
 
# printing result
print("The consecutive element frequency is : " + str(res))


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

Time complexity: O(N) where N is the length of the input list since we iterate over the list only once.
Auxiliary space: O(N) since we need to store the result in a list of tuples with at most N elements.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads