delete duplicates in list

Dave Benjamin ramen at lackingtalent.com
Mon Nov 3 00:34:43 EST 2003


In article <slrnbqbpqt.vf5.ramen at lackingtalent.com>, Dave Benjamin wrote:
> In article <mailman.285.1067612751.702.python-list at python.org>, Martin Chilvers wrote:
>> Maybe I'm just old-school, and it won't win any prizes for speed but...
>> 
>>>>> a = [1, 3, 2, 3, 4, 2, 1, 4, 3, 2, 1, 2, 4]
>>>>> reduce(lambda l, x: x not in l and l.append(x) or l, a, [])
>> [1, 3, 2, 4]
>> 
>> ;^)
> 
> Hey, I wanna play this game too:
> 
> [d.setdefault(x, x) for d in [{}] for x in a if x not in d]
> 
> Now, try replacing that with "old-school" functionals. I'm stumped. =)

Nevermind, I'm so there... ;)

>>> filter(lambda x, d={}: x not in d and d.setdefault(x, True), a)
[1, 3, 2, 4]

Corrupting Python since '02,
Dave




More information about the Python-list mailing list