Removing duplicates from a list

martijn at gamecreators.nl martijn at gamecreators.nl
Thu Sep 15 10:08:52 EDT 2005


Look at the code below

def unique(s):
    return list(set(s))

def unique2(keys):
    unique = []
    for i in keys:
        if i not in unique:unique.append(i)
    return unique

tmp = [0,1,2,4,2,2,3,4,1,3,2]
print tmp
print unique(tmp)
print unique2(tmp)
--------------------------
[0, 1, 2, 4, 2, 2, 3, 4, 1, 3, 2]
[0, 1, 2, 3, 4]
[0, 1, 2, 4, 3]

As you can see the end result is not the same.
I must get the end result [0, 1, 2, 4, 3] and not [0, 1, 2, 3, 4].
Thats why I use unique2()




More information about the Python-list mailing list