Python find duplicates which occur more than 3 times

  • A+
Category:Languages

I am trying to find an efficient way to search three or more consecutive duplicates and replace them for only one in a Python list.

list_before = [1, 1, 1, 2, 3, 4, 5, 5, 5, 6, 6, 7, 7, 7, 8, 8, 8, 8, 8]  # expected list_after = [1, 2, 3, 4, 5, 6, 6, 7, 8]  def replace(list_to_replace):     for idx, val in enumerate(list_to_replace):         if idx + 3 < len(list_to_replace):             if val == list_to_replace[idx+1] == list_to_replace[idx+2]:                 del list_to_replace[idx+1]                 del list_to_replace[idx+2]     return list_to_replace  >>> replace(list_before) [1, 1, 3, 4, 5, 5, 6, 7, 7, 8, 8, 8] 

What seems to be the problem here? Is there a more efficient way?

 


I good use case for itertools.groupby:

>>> from itertools import groupby >>> list_before = [1, 1, 1, 2, 3, 4, 5, 5, 5, 6, 6, 7, 7, 7, 8, 8, 8, 8, 8] >>> list_after = [] >>> for k, group in groupby(list_before): ...     lst = list(group) ...     if len(lst) >= 3: ...         list_after.append(k) ...     else: ...         list_after.extend(lst) >>> list_after [1, 2, 3, 4, 5, 6, 6, 7, 8] 

It would be possible make a one-liner with itertools.chain but the for loop is almost certainly more readable and similarly performant.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: