[Tutor] Ways of removing consequtive duplicates from a list

Peter Otten __peter__ at web.de
Sun Jul 17 04:26:37 EDT 2022


On 17/07/2022 00:01, Alex Kleider wrote:

> PS My (at least for me easier to comprehend) solution:
>
> def rm_duplicates(iterable):
>      last = ''
>      for item in iterable:
>          if item != last:
>              yield item
>              last = item

The problem with this is the choice of the initial value for 'last':

 >>> list(rm_duplicates(["", "", 42, "a", "a", ""]))
[42, 'a', '']   # oops, we lost the initial empty string

Manprit avoided that in his similar solution by using a special value
that will compare false except in pathological cases:

 > val = object()
 > [(val := ele) for ele in lst if ele != val]

Another fix is to yield the first item unconditionally:

def rm_duplicates(iterable):
     it = iter(iterable)
     try:
         last = next(it)
     except StopIteration:
         return
     yield last
     for item in it:
         if item != last:
             yield item
             last = item

If you think that this doesn't look very elegant you may join me in the
https://peps.python.org/pep-0479/ haters' club ;)


More information about the Tutor mailing list