[Tutor] Eliminating consecutive duplicates in a list

Emile van Sebille emile at fenx.com
Thu Jun 18 17:02:07 CEST 2009


On 6/18/2009 7:23 AM karma said...
> Hi Robert,
> 
> Thanks for the link. However, I am looking for eliminating consecutive
> duplicates rather than all duplicates - my example wasn't clear,
> apologies for that.
> 
> x=[1,1,1,3,2,2,2,4,4,2,2]
> 
> [1 ,3 ,2 ,4 ,2 ]

Something like

[ ii for ii,jj in zip(x,x[1:]+[-1]) if jj != ii]

Emile



More information about the Tutor mailing list