Sometimes, while working with Python lists or in competitive programming setup, we can come across a subproblem in which we need to get an element which has the maximum consecutive occurrence. The knowledge of solution of it can be of great help and can be employed whenever required. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using
groupby() + max() + lambda
This task can be solved using combination of above functions. In this, we group each occurrence of numbers using
groupby() and get the max of it using
max(). The lambda function provide utility logic to perform this task.
The original list is : [1, 1, 1, 2, 2, 4, 2, 2, 5, 5, 5, 5] Maximum Consecutive Occurring number is : 5
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Python - Maximum Consecutive Substring Occurrence
- Python - Consecutive Alphabetic Occurrence
- Python | Last occurrence of some element in a list
- Python | Count occurrence of all elements of list in a tuple
- Python | Element Occurrence in dictionary of list values
- Python | Count tuples occurrence in list of tuples
- Python - Occurrence counter in List of Records
- Python - Sum of each List element occurrence in another
- Python - Nearest occurrence between two elements in a List
- Python program to remove each y occurrence before x in List
- Python - Maximum of Consecutive Pair in integer list
- Generate two output strings depending upon occurrence of character in input string in Python
- Python | Replace multiple occurrence of character by single
- Python | First occurrence of True number
- Python | Percentage occurrence at index
- Python | Sort tuple based on occurrence of first element
- Python | Add the occurrence of each number as sublists
- Python program to find occurrence to each character in given string
- Python | Find last occurrence of substring
- Python | Get the string after occurrence of given substring
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.