unique-ifying a list

ryles rylesny at gmail.com
Sat Aug 8 06:58:03 EDT 2009


On Aug 7, 4:53 pm, kj <no.em... at please.post> wrote:
> Suppose that x is some list.  To produce a version of the list with
> duplicate elements removed one could, I suppose, do this:
>
>     x = list(set(x))
>
> but I expect that this will not preserve the original order of
> elements.
>

OrderedSet is most likely on the way, but for now Python 3.1 and 2.7
have OrderedDict. For 3.0 and 2.6 the recipe is here:

http://code.activestate.com/recipes/576669

With OrderedDict you can do:

OrderedDict.fromkeys(x).keys()  # Returns an iterator in 3.x, a list
in 2.x.



More information about the Python-list mailing list