[Tutor] removing consecutive duplicates from list

Manprit Singh manpritsinghece at gmail.com
Tue Apr 20 12:48:11 EDT 2021


Dear sir ,

Consider a list given below:
lst = [2, 3, 3, 4, 5, 5, 3, 7, 9, 9, 4]
i need to remove consecutive duplicates from list lst:
the answer must be :

[2, 3, 4, 5, 3, 7, 9, 4]

The code that i have written to solve it, is written below:
lst = [2, 3, 3, 4, 5, 5, 3, 7, 9, 9, 4]
ls = lst[1:]+[object()]
[x for x, y in zip(lst, ls) if x != y]

The list comprehension gives the desired result. just need to know if this

program can be done in a more readable and less complex way.


More information about the Tutor mailing list