Editor

Remove Duplicates

Lists · Intermediate · 60 XP · about 7 min

Write a function called remove_duplicates(lst) that returns a new list with duplicate elements removed, preserving the original order.

Example

remove_duplicates([1, 2, 2, 3, 1, 4])    # Returns [1, 2, 3, 4]
remove_duplicates(["a", "b", "a", "c"])  # Returns ["a", "b", "c"]
remove_duplicates([1, 1, 1])             # Returns [1]

Requirements

  • Return a new list without duplicates
  • Preserve the order of first occurrence
  • Handle both numbers and strings
def remove_duplicates(lst):
# Your code here
pass
# Test your function
print(remove_duplicates([1, 2, 2, 3, 1, 4]))
print(remove_duplicates(["a", "b", "a", "c"]))

Ready?

Accept the challenge to start writing. A small timer counts the time this page is in view.

Output appears here.