fork download
  1. def remove_duplicates(input_list):
  2. """
  3. Removes duplicate elements from a list while preserving the original order.
  4.  
  5. Args:
  6. input_list: The list to remove duplicates from.
  7.  
  8. Returns:
  9. A new list with duplicates removed.
  10. """
  11. new_list = [] # Create an empty list to store unique elements
  12. seen = set() # Create an empty set to keep track of elements we've seen
  13.  
  14. for element in input_list:
  15. if element not in seen: # If we haven't seen this element before
  16. new_list.append(element) # Add it to the new list
  17. seen.add(element) # And mark it as seen
  18.  
  19. return new_list
  20.  
  21. # Example usage:
  22. my_list = [1, 2, 2, 3, 4, 4, 5]
  23. result = remove_duplicates(my_list)
  24. print(result) # Output: [1, 2, 3, 4, 5]
Success #stdin #stdout 0.01s 7228KB
stdin
Standard input is empty
stdout
[1, 2, 3, 4, 5]