Sets: unique, sorted
Read the task → edit your code → Run to see the output → Run tests to check your answer.
A set is an unordered collection that automatically throws away duplicates:
set([3, 1, 2, 3, 1]) # {1, 2, 3}
len({1, 1, 1}) # 1
That makes "remove duplicates" almost free. Sets have no order, so when you
need a predictable result you hand them to sorted(), which returns a new
list in ascending order.
Your task
Write unique_sorted(values) that returns a list of the distinct numbers
in values, sorted from smallest to largest.
unique_sorted([3, 1, 2, 3, 1]) # [1, 2, 3]
unique_sorted([]) # []
unique_sorted([5, 5, 5]) # [5]
Hint: one built-in throws away duplicates, another turns any collection into an ascending list. Reach for both and feed one into the other — then double-check you're handing back a list, not a set.
Tests
- dedupe + sort
- empty
- all same
- negatives
✦ Need help with this task?
Sign in or create a free account to get AI help. Free includes a monthly AI allowance.
$ Console output will appear here.