Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Convert a list to a set to remove duplicates

Learn how to remove duplicate items from a list by converting it to a set and returning a canonical sorted result.

Python practice8 minDictionaries & SetsBeginnerLast updated March 18, 2026

Problem statement

Given a list of hashable and comparable items, remove any duplicates by converting the list to a set, then return the unique items as a sorted list. This provides a canonical ordering for testing and predictable output.

Task

Write a function that removes duplicates from a list by using a set and returns a sorted list of the unique items.

Examples

Remove duplicates from integers

Input

list_to_set([3, 1, 2, 3])

Output

[1, 2, 3]

Convert to a set to drop the duplicate 3, then sort to get a consistent list [1, 2, 3].

Input format

A single list of hashable, comparable items (numbers or strings).

Output format

A sorted list containing each unique item from the input exactly once.

Constraints

Elements in the list are hashable and mutually comparable (so sorting works). List length can be large (e.g., up to 100000), so aim for reasonable time/space usage.

Samples

Sample 1

Input

[1, 1, 2, 2, 3]

Output

[1, 2, 3]

Duplicates removed and result sorted.