Convert List to Set in Python: Remove Duplicates Safely

To convert a list to a set in Python, pass the list to set(). This removes duplicate values and returns a set of unique hashable items.

Use this when order does not matter and you need fast membership checks or unique values. If order matters, use dict.fromkeys() and convert the keys back to a list.

Quick Answer

Use set(my_list) for the simplest conversion. Python documents sets in the official set types reference and in the tutorial section on sets.

numbers = [3, 1, 3, 2, 1]
unique_numbers = set(numbers)

print(unique_numbers)

The result contains each value once. The display order of a set is not something your code should rely on.

Convert a List to a Set

A list can contain duplicate values, while a set keeps only unique values. Python’s standard docs describe lists as mutable sequences.

items = ["red", "blue", "red", "green"]
unique_items = set(items)

print(unique_items)
print("red" in unique_items)

Sets are useful when you repeatedly check whether a value exists. Membership checks in a set are usually clearer and faster than scanning a long list.

Convert Back to a List

If later code needs a list, wrap the set with list(). This gives you unique values, but not the original order.

numbers = [5, 2, 5, 1, 2]
unique_list = list(set(numbers))

print(unique_list)

Use this only when order is irrelevant. If order matters, use the next pattern instead.

Remove Duplicates and Preserve Order

To remove duplicates while preserving first-seen order, use dict.fromkeys(). Python documents dict.fromkeys(), and dictionaries are covered in the tutorial section on dictionaries.

numbers = [5, 2, 5, 1, 2]
unique_in_order = list(dict.fromkeys(numbers))

print(unique_in_order)

This returns [5, 2, 1]. It is often the best method for user-facing lists where the first occurrence should stay in place.

Convert a List of Strings to a Set

String values work well in sets because strings are hashable. Normalize case first if "Python" and "python" should be treated as the same value.

tags = ["Python", "python", "SEO", "seo"]
unique_tags = {tag.lower() for tag in tags}

print(unique_tags)

For related cleanup, see PythonPool’s Python lowercase, remove newline from list, and count words in a string guides.

Handle Unhashable Values

Only hashable values can go into a set. Numbers, strings, and tuples are usually fine. Lists and dictionaries are mutable, so they are not hashable.

rows = [["Ada", 1], ["Ada", 1], ["Grace", 2]]
unique_rows = {tuple(row) for row in rows}

print(unique_rows)

Convert inner lists to tuples when the row content should become set items. For nested mapping work, see PythonPool’s nested dictionary guide.

Use Sets for Membership Tests

Sometimes you do not need to convert the set back to a list. Keep it as a set when the main job is lookup.

allowed = set(["read", "write", "delete"])

print("write" in allowed)
print("admin" in allowed)

This pattern is common for permissions, filters, known IDs, and validation lists. If you need to copy or mutate lists before conversion, PythonPool’s copy list, list pop(), and dynamic array guides are useful follow-ups.

Sort Unique Values When Output Order Matters

If you want unique values in sorted order, convert to a set and then call sorted(). This returns a list.

numbers = [4, 1, 4, 3, 1, 2]
unique_sorted = sorted(set(numbers))

print(unique_sorted)

For dictionary sorting patterns, see PythonPool’s sort dictionary by key article. For size and memory context, read dictionary size.

When Not to Convert to a Set

Do not convert a list to a set when duplicate counts matter, when the original order is part of the meaning, or when the list contains unhashable nested values that should remain mutable. A set is a uniqueness tool, not a drop-in replacement for every list.

If you need duplicate counts, use a counting approach instead of a set. If you need a unique list in a stable order, dict.fromkeys() is usually safer than list(set(values)).

Common Mistakes

  • Expecting a set to preserve the original list order.
  • Trying to put lists or dictionaries directly inside a set.
  • Using a set when duplicate counts are important.
  • Converting back to a list and assuming the result is sorted.
  • Forgetting that True and 1 compare as equal in a set.

FAQs

How do I convert a list to a set in Python?

Use set(my_list). It returns a set containing unique values from the list.

Does converting a list to a set preserve order?

No. Use list(dict.fromkeys(my_list)) when you need unique values in first-seen order.

Why do I get unhashable type: list?

A set can only contain hashable values. Convert inner lists to tuples, or choose a different data structure.

Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Alistair Windsor
Alistair Windsor
4 years ago

I dont know any way to guarantee O(n) running time here. The issue is that when you pull an element you have to examine the existing set to see if it is already present. Using a hashtable implementation you gave expected lookup O(1) but worst case O(n). That gives you worst case runtime of O(n^2). It is simple to get the worst time down to O(n log n) with say a balanced tree but hash tables will typically be faster.

Pratik Kinage
Admin
4 years ago

I believe adding elements to set is of O(1) complexity (hash sets).

Regards,
Pratik