easy +10 pts

All Unique Values Keep Order

Remove duplicates from a list while preserving the original order of first occurrences.

Write a function `unique_keep_order(lst)` that takes a list `lst` and returns a new list containing only the distinct elements of `lst`, in the order they first appear. For example, given `[3, 1, 3, 2, 1]`, the result should be `[3, 1, 2]`. The original list should not be modified. Your function must handle lists with 0 or more elements. Elements can be of any hashable type (e.g., integers, strings, tuples).

Constraints

0 <= len(lst) <= 10^5. The function should run in O(n) time and use O(n) extra space.

Example

>>> unique_keep_order([3, 1, 3, 2, 1])
[3, 1, 2]
>>> unique_keep_order(['a', 'b', 'a', 'c', 'b'])
['a', 'b', 'c']
>>> unique_keep_order([])
[]
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use a set to track which elements have already been seen.
Iterate through the list and only append an element to the result if it is not already in the set.
Remember to add each element to the set after checking/appending.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.