faqts : Computers : Programming : Languages : Python : Common Problems : Lists

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

25 of 29 people (86%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

Is there a built in Python function that eliminates duplicates in a list?

Jun 27th, 2000 06:16
unknown unknown, Bjorn Pettersen, Aahz Maruch, jerry_spicklemire


def unique(lst):
    d = {}
    for item in lst:
        d[item] = None
    return d.keys()

--------

The Timbot recommends

        d[item] = 1

because there's no lookup involved on 1, so it's fractionally faster.
(Any other constant should probably work the same.)

--------

Just a minor "heads up" for a possible "gotcha".

It looks to me as if this method, though speedy, will return the
cleaned list in a much different order than the original, since the

  dict.keys()

method outputs in the (seemingly random) order of the dicts internal
hash table.

The moral is, if the order of the list members matters, be sure to wait
until after the cleaning step to sort, or at least remember to re-sort!