- Trending Categories
- Data Structure
- Operating System
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python – Consecutive identical elements count
When it is required to get the count of consecutive identical elements in a list, an iteration, the ‘append’ method, and the ‘set’ method are used.
Below is a demonstration of the same
my_list = [24, 24, 24, 15, 15, 64, 64, 71, 13, 95, 100] print("The list is :") print(my_list) my_result =  for index in range(0, len(my_list) - 1): if my_list[index] == my_list[index + 1]: my_result.append(my_list[index]) my_result = len(list(set(my_result))) print("The result is :") print(my_result)
The list is : [24, 24, 24, 15, 15, 64, 64, 71, 13, 95, 100] The result is : 3
A list is defined and is displayed on the console.
An empty list is defined.
The list is iterated over and if the element in the zeroth index and element in the first index are equivalent, the zeroth element is appended to the empty list.
This is converted to a set and then to a list, and its length is assigned to a variable.
This is the output that is displayed on the console.
- Program to count operations to remove consecutive identical bits in Python
- Python program to count pairs for consecutive elements
- Python – Filter consecutive elements Tuples
- Python – Reorder for consecutive elements
- Swap Consecutive Even Elements in Python
- Python – Group Consecutive elements by Sign
- Python – Summation of consecutive elements power
- Count Subarrays with Consecutive elements differing by 1 in C++
- Consecutive elements pairing in list in Python
- Python – Grouped Consecutive Range Indices of Elements
- Python - Check if all elements in a list are identical
- Check if array elements are consecutive in Python
- Check if Queue Elements are pairwise consecutive in Python