how to remove duplicated elements in a list?

Brian van den Broek broek at cc.umanitoba.ca
Mon Dec 19 12:44:37 EST 2005


Alex Martelli said unto the world upon 2005-12-19 10:48:
> Brian van den Broek <broek at cc.umanitoba.ca> wrote:
>    ...
> 
>>>>>orig_list = [3,1,2,3,1,2,3,1,2,1,2,1,3]
>>>>>new_list = list(set(orig_list))
>>>>>new_list.sort(cmp= lambda x,y: cmp(orig_list.index(x), 
>>
>>orig_list.index(y)))
>>
>>>>>new_list
>>
>>[3, 1, 2]
>>
> 
> A better way to exploit exactly the same idea is to use:
> 
> new_list.sort(key=orig_list.index)
> 

Thanks, Alex.

This time, I knew to try the new'ish key argument, but I couldn't 
quite see how to do it. Now you've shown it -- Doh!

Best,

Brian vdB




More information about the Python-list mailing list