[Tutor] Ways of removing consequtive duplicates from a list

Alex Kleider alexkleider at gmail.com
Sat Jul 16 18:01:24 EDT 2022


On Sat, Jul 16, 2022 at 12:54 PM <avi.e.gross at gmail.com> wrote:
>
> Manprit,
>
> Your message is not formatted properly in my email and you just asked any
> women present to not reply to you, nor anyone who has not been knighted by a
> Queen. I personally do not expect such politeness but clearly some do.
>

I confess it took me longer than it should have to figure out to what
you were referring in the second half of the above but eventually the
light came on and the smile blossomed!
My next thought was that it wouldn't necessarily have had to have been
a Queen although anyone knighted (by a King) prior to the beginning of
our current Queen's reign is unlikely to be even alive let alone
interested in this sort of thing.
Thanks for the morning smile!
a
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

lst = [2, 2, 3, 3, 3, 2, 2, 5, 5, 6, 3, 3, 3, 3]

if __name__ == '__main__':
    res = [res for res in rm_duplicates(lst)]
    print(res)
    assert res == [2, 3, 2, 5, 6, 3]

-- 
alex at kleider.ca  (sent from my current gizmo)


More information about the Tutor mailing list