[Tutor] how to remove the coming duplication

Steven D'Aprano steve at pearwood.info
Thu Nov 10 13:48:48 CET 2011


lina wrote:
> Hi,
> 
> How to remove the coming duplication,
> 
> Here I wrote one (not working):
> 
> 
>>>> a=['2', '5', '7', '5', '5']
[...]
> I wish to get a is [2,5,7,5]
> 
> just remove the coming duplication, not unique the list.

a = [2, 5, 7, 5, 5]
b = a[0:1]  # slice of the first item only
for x in a[1:]:  # slice of the second item to the end
     if x != b[-1]:
         b.append(x)


gives b = [2, 5, 7, 5] as requested.



-- 
Steven


More information about the Tutor mailing list