List without duplicates?
Jeremy Hylton
jeremy at beopen.com
Wed Jun 21 14:24:41 EDT 2000
Wolfgang Grafen <Wolfgang.Grafen at marconicomms.com> writes:
> > Is there a built in Python function that eliminates duplicates in a list?
> Try this ...
>
> old_list = [1, 2, 3, 2, 1, 4, 3, 5, 2]
>
>
> unique_list = []
>
> for member in old_list:
> if not member in unique_list:
> unique_list.append(member)
>
This is a bad way to do it. The "not member in unique_list" test
involves a linear search of unique_list. This gives your uniquify
function O(n^2) behavior. The dictionary version posted earlier uses
the dictionary's hash table and achieves constant time for the test.
It will perform much better.
Jeremy
More information about the Python-list
mailing list