Iād avoid sorting the value passed. If youāre returning the filtered version, why should I expect value passed to be mutated? With this in mind, shouldnāt the result be in the same order as the source, sans dups?
def remove_duplicates(xs): # list is a horrid name for a var in python
result = []
for x in xs:
if x not in result:
result.append(x)
return result
print(remove_duplicates([5,4,4,5]))
You can actually get kind of clever with this, if youāre inclined, using a list comprehension.
def remove_duplicates(xs):
return [x for (idx,x) in enumerate(xs) if x not in xs[:idx]]
Note, that list comprehension can be used as a straight up copy [x for x in xs]
.
It occurs to me that you could do a silly, but interesting, a sieve of eratosthenes kind of thing, and avoid the in
check.
def remove_duplicates(xs):
sieve = [x for x in xs] # make that copy
for i in range(len(xs)):
if sieve[i] is not None:
# replace all other occurances with None
for j in range(i + 1, len(xs)):
if sieve[j] == sieve[i]:
sieve[j] = None
return [x for x in sieve if x is not None] # return the not None
While you pay for the copy up front, I suspect this would be the quicker one on larger setsā¦
Sets?
Ok, weād be remiss if we didnāt mention the python built in for this; order not maintained.
def remove_duplicates(xs):
return list(set(xs))
The above also helpfully shows why list
is a bad idea for a variable name. Never shadow function names, it will only cause pain and suffering.