Answer to Question #197870 in Python for LEROY

Question #197870

 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]. 


1
Expert's answer
2021-05-25T18:01:50-0400
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))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS