Write a program using function called remove_duplicates that takes a list and returns a new list with only the unique elements from the original list. For example remove_duplicates([1, 2, 3, 3, 4, 2, 1, 5]) would return [1, 2, 3, 4, 5].
def remove_duplicates(L):
LS = sorted(L)
res = []
prev = None
for el in LS:
if el != prev:
res.append(el)
prev = el
return res
L = [1, 2, 3, 3, 4, 2, 1, 5]
print(remove_duplicates(L))
Comments
Leave a comment