[Tutor] Eliminating consecutive duplicates in a list
dorjetarap at googlemail.com
Thu Jun 18 16:23:28 CEST 2009
Thanks for the link. However, I am looking for eliminating consecutive
duplicates rather than all duplicates - my example wasn't clear,
apologies for that.
[1 ,3 ,2 ,4 ,2 ]
2009/6/18 Robert Berman <bermanrl at cfl.rr.com>:
> This might help: http://code.activestate.com/recipes/52560/
> On Thu, 2009-06-18 at 15:15 +0200, karma wrote:
>> I was playing around with eliminating duplicates in a list not using
>> groupby. From the two solutions below, which is more "pythonic".
>> Alternative solutions would be welcome.
>> [v for i,v in enumerate(x) if x[i]!=x[i-1] or i==0]
>> [x[i] for i in range(len(x)-1) if i==0 or x[i]!=x[i-1]]
>> [1, 3, 2, 4]
>> Tutor maillist - Tutor at python.org
More information about the Tutor