How to construct a set out of list items in python?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Python, the standard way to construct a set from a list is to pass the list into the set() constructor. That gives you a collection of unique elements and is one of the most common ways to remove duplicates quickly.
The basic operation is simple, but there are a few important details: sets are unordered, all elements must be hashable, and sometimes a set comprehension is a better fit than a direct conversion.
The Basic Conversion
The direct conversion looks like this:
This removes duplicates automatically. The result might print in a different order from the input because sets are unordered collections.
Why Use a Set
Converting a list to a set is useful when you care about:
- uniqueness,
- fast membership checks,
- or later set operations such as intersection and difference.
For example:
Membership testing on a set is typically much faster than scanning a list repeatedly.
Using a Set Comprehension
If you want to transform items while building the set, use a set comprehension.
This is often better than first building an intermediate list and then converting it.
Set comprehensions are especially useful when:
- the input needs normalization,
- you want to filter some items,
- or the final set is not a direct copy of the input list.
Constructing a Set of Derived Values
You can also use a comprehension to derive values:
That is still a set, not a list. Duplicate derived values are collapsed automatically.
Be Careful With Unhashable Items
Not every list can be turned directly into a set. Set elements must be hashable, which means mutable containers such as lists and dictionaries are not allowed as elements.
This fails:
Python raises:
If the elements are lists but you want set behavior, convert each inner list to a tuple first:
That works because tuples are hashable when their contents are hashable.
Removing Duplicates While Preserving Order
Sometimes people say they want a set, but what they really want is duplicate removal while preserving the original order. A plain set does not preserve the input order semantics you probably expect.
If order matters, a common pattern is:
That returns a list, not a set, but it preserves first occurrence order.
So the real question is often:
- do you want uniqueness only, or
- uniqueness plus input order
The answer determines whether set() is the right tool.
Building a Set Incrementally
You can also start with an empty set and add items:
This is useful when values arrive over time rather than all at once.
Common Pitfalls
One common mistake is expecting a set to preserve list ordering. It does not behave like an ordered list of unique items.
Another mistake is trying to put mutable values such as lists or dictionaries into a set. Those are unhashable and cannot be added directly.
It is also easy to choose a set when duplicates actually matter. If the count of repeated items is important, use a list or collections.Counter instead.
Finally, if you only need a one-time duplicate check, converting a huge list to a set may be wasteful if another streaming approach would do.
Summary
- Use
set(my_list)to build a set from a list in the simplest case. - Use a set comprehension when you want to transform or filter items during construction.
- Set elements must be hashable, so nested lists must be converted first.
- A set removes duplicates but does not preserve the meaningful order of the original list.
- If you need uniqueness and order, you may want an ordered list-based solution instead of a plain set.

