HOWTO · Python

How to Convert List to Set in Python

It demonstrates how to convert a list into a set in Python.

On this page

To convert a list to a set, pass it to Python’s built-in set() constructor, for example set(my_list).

The result contains each distinct hashable value once. A list keeps its positions and repeated values; a set is mutable, contains unique hashable elements, and does not promise an iteration order. Use a set when uniqueness and membership checks matter more than position or repetition.

Why converting a list to a set changes the data

A list such as ["tea", "coffee", "tea"] records three entries in a defined order. Converting it to a set retains the distinct values, but discards the repeated "tea" and the list’s positional structure. A set can contain values of different types when each value is hashable, such as strings, numbers, and tuples of hashable values. The set itself can be changed, but its elements must be hashable; lists and dictionaries cannot be elements.

Convert a list to a set with set()

Call set(iterable) to construct a set from an iterable such as a list or tuple. With unique input such as [1, 2, 3], conversion retains all three values. A mixed list such as [1, "two", (3, 4)] is also valid because integers, strings, and tuples of hashable values can coexist in a set. These examples convert both inputs; membership checks make the mixed result order-independent:

unique_values = [1, 2, 3]
unique_set = set(unique_values)
assert unique_set == {1, 2, 3}

mixed_values = [1, "two", (3, 4)]
mixed_set = set(mixed_values)
assert len(mixed_set) == 3
assert 1 in mixed_set and "two" in mixed_set and (3, 4) in mixed_set

The assertions check the set contents without depending on iteration order.

Use this example to see how repeated input values affect the number of distinct results:

Run this example:

values = [3, 1, 3, 2]
converted = set(values)
print(len(converted))
3

The conversion keeps one copy of each value, so the length is the count of distinct input values. This count is deterministic even though printing the set itself could show values in different orders. Calling set() without an argument creates an empty set.

Handle duplicates, ordering, and unhashable items

Duplicate values collapse during conversion. Do not rely on the order in which a set iterates, and do not use indexing or slicing on it. If you need unique values while preserving their first-seen order, keep a list and track seen values separately, or choose an order-preserving approach that fits your data.

Every element passed to set() must be hashable. A list containing another list, for example, cannot be converted directly because the nested list is unhashable; Python raises TypeError. If the nested values represent records, decide which hashable key should define uniqueness, then build the set from those keys or from suitable immutable representations. Tuples work only when all their contents are hashable.

When converting a list to a set is appropriate

Convert when duplicates are unwanted, order does not matter, and all values are hashable. This is useful for removing repeated tags or checking whether a value appears among distinct choices. Keep the original list if later code depends on its order, its repeated entries, or positions associated with each value. Converting back with list(my_set) creates a list, but it cannot restore repetitions or the original order that the set discarded.