# Python – Consecutive identical elements count

• Last Updated : 12 Nov, 2020

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
```

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
```

My Personal Notes arrow_drop_up