Open In App

Python – Consecutive identical elements count

Improve
Improve
Like Article
Like
Save
Share
Report

Given the elements list, get all the elements that have an identical element as the next element.

Input : test_list = [4, 5, 5, 5, 5, 6, 6, 7, 8, 2, 2, 10] 
Output : 3 
Explanation : 5, 6 and 2 has identical element as their next element.

Input : test_list = [4, 5, 5, 5, 5, 6, 6, 7, 8, 2, 3, 10] 
Output : 2 
Explanation : 5 and 6 has identical element as their next element. 

Method #1 : Using loop + set()

In this, we iterate and check for the next element, if equal to current, we add in result list, then it is converted to set to get distinct elements.

Python3




# Python3 code to demonstrate working of
# Consecutive identical elements count
# Using loop + set()
 
# initializing list
test_list = [4, 5, 5, 5, 5, 6, 6, 7, 8, 2, 2, 10]
 
# printing original list
print("The original list is : " + str(test_list))
 
res = []
for idx in range(0, len(test_list) - 1):
     
    # getting Consecutive elements
    if test_list[idx] == test_list[idx + 1]:
        res.append(test_list[idx])
         
# getting count of unique elements       
res = len(list(set(res)))
 
# printing result
print("Consecutive identical elements count : " + str(res))


Output

The original list is : [4, 5, 5, 5, 5, 6, 6, 7, 8, 2, 2, 10]
Consecutive identical elements count : 3

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

Method #2 : Using list comprehension + set() + len()

This method is similar to above method, difference being its shorter way to solve this problem. The len(), and set() are used for task of getting unique elements count.

Python3




# Python3 code to demonstrate working of
# Consecutive identical elements count
# Using list comprehension + set() + len()
 
# initializing list
test_list = [4, 5, 5, 5, 5, 6, 6, 7, 8, 2, 2, 10]
 
# printing original list
print("The original list is : " + str(test_list))
 
# getting Consecutive elements
res = [test_list[idx] for idx in range(
    0, len(test_list) - 1) if test_list[idx] == test_list[idx + 1]]
 
# getting count of unique elements
res = len(list(set(res)))
 
# printing result
print("Consecutive identical elements count : " + str(res))


Output

The original list is : [4, 5, 5, 5, 5, 6, 6, 7, 8, 2, 2, 10]
Consecutive identical elements count : 3

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

Method #3: Using itertools.groupby()

  • First, we import the itertools module which provides various functions for efficient looping
  • We create a list named test_list which contains 12 elements.
  • We print the original list using print() function.
  • Using itertools.groupby() function, we group the consecutive identical elements in the list into subgroups. The itertools.groupby() function returns an iterator that generates consecutive keys and groups from an iterable. In this case, the iterable is the test_list.
  • We use a list comprehension with a condition to count the number of groups that have a length greater than 1 (which means there are consecutive identical elements).
  • We use the sum() function to sum up the number of groups that satisfy the condition in the list comprehension.
  • We print the count of consecutive identical elements using print() function.

Python3




import itertools
 
# initializing list
test_list = [4, 5, 5, 5, 5, 6, 6, 7, 8, 2, 2, 10]
 
# printing original list
print("The original list is : " + str(test_list))
 
# getting Consecutive elements using itertools.groupby()
res = sum(1 for _, grp in itertools.groupby(test_list) if len(list(grp)) > 1)
 
# printing result
print("Consecutive identical elements count : " + str(res))


Output

The original list is : [4, 5, 5, 5, 5, 6, 6, 7, 8, 2, 2, 10]
Consecutive identical elements count : 3

Time complexity: O(n) – The itertools.groupby() function iterates through the entire input list once, which takes O(n) time.
Auxiliary space: O(1) – The itertools.groupby() function does not create any additional data structures that depend on the size of the input list. 

Method #4: Using a Counter object from the collections module

Step-by-step approach:

  • Import the Counter class from the collections module.
  • Create a Counter object by passing the test_list to it.
  • Iterate over the items in the Counter object and count the number of elements with frequency greater than 1.
  • Print the count of consecutive identical elements.

Python3




from collections import Counter
 
# initializing list
test_list = [4, 5, 5, 5, 5, 6, 6, 7, 8, 2, 2, 10]
 
# printing original list
print("The original list is : " + str(test_list))
 
# creating Counter object
counter_obj = Counter(test_list)
 
# counting consecutive identical elements
res = sum(1 for freq in counter_obj.values() if freq > 1)
 
# printing result
print("Consecutive identical elements count : " + str(res))


Output

The original list is : [4, 5, 5, 5, 5, 6, 6, 7, 8, 2, 2, 10]
Consecutive identical elements count : 3

Time complexity: O(n), where n is the length of the list.
Auxiliary space: O(n), for storing the Counter object



Last Updated : 10 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads