Best way to make a list unique?

Delaney, Timothy C (Timothy) tdelaney at avaya.com
Tue Mar 8 17:41:41 EST 2005


Diez B. Roggisch wrote:

> No. But I doubt that that is what you actually want, as listA will
> lose its order afterwards. Typically, something like that gets
> written like this: 

This is actually one thing that Java 1.5 has that I'd like to see in
Python - the LinkedHashSet and LinkedHashMap. Very useful data
structures.

For those who don't know, these implement a hash set/map which iterates
in the order that the keys were first added to the set/map.

Such a data structure is fairly simple to implement, and removing
duplicates then becomes:

    unique = list(linkedset(orig))

and you're guaranteed to maintain order.

Implementing these is fairly simple.

Tim Delaney



More information about the Python-list mailing list