Python – Consecutive elements maximum frequencies

• Last Updated : 02 Jun, 2020

Sometimes, while working with Python lists we can have a problem in which we need to extract the maximum frequencies of consecutive elements. This kind of problem can occur as application in many domains such as day-day programming and competitive programming. Let’s discuss certain ways in which this task can be performed.

Input : test_list = [7, 6, 7, 7]
Output : {7: 2, 6: 1}

Input : test_list = [7, 7, 7]
Output : {7: 3}

Method #1 : Using loop + groupby()
The combination of above functions can be used to perform this task. In this, we group all the consecutive elements and then generate the dictionary of maximum frequencies using dictionary keys comparison with current maximum.

 # Python3 code to demonstrate working of # Consecutive elements maximum frequencies# Using loop + groupby()from itertools import groupby  # initializing listtest_list = [5, 6, 7, 7, 6, 6, 5, 7]  # printing original listprint("The original list is : " + str(test_list))  # Consecutive elements maximum frequencies# Using loop + groupby()res = {}for key, val in groupby(test_list):    sub = sum(1 for _ in val)    if res.get(key, float('-inf')) < sub:        res[key] = sub  # printing result print("The maximum frequencies : " + str(res))
Output :
The original list is : [5, 6, 7, 7, 6, 6, 5, 7]
The maximum frequencies : {5: 1, 6: 2, 7: 2}

Method #2 : Using max() + list comprehension + set() + groupby()
The combination of above functions can be used to solve this particular problem. In this, we use list comprehension to perform comparisons and maximum element is found using max().

 # Python3 code to demonstrate working of # Consecutive elements maximum frequencies# Using max() + list comprehension + set() + groupby()from itertools import groupby  # initializing listtest_list = [5, 6, 7, 7, 6, 6, 5, 7]  # printing original listprint("The original list is : " + str(test_list))  # Consecutive elements maximum frequencies# Using max() + list comprehension + set() + groupby()temp = list(set(test_list))res = [{ele : max([len(list(val)) for key, val in groupby(test_list)                                  if ele == key])} for ele in temp]  # printing result print("The maximum frequencies : " + str(res))
Output :
The original list is : [5, 6, 7, 7, 6, 6, 5, 7]
The maximum frequencies : [{5: 1}, {6: 2}, {7: 2}]

My Personal Notes arrow_drop_up