python
set
list
programming
tutorial

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:

python
1numbers = [1, 2, 2, 3, 4, 4, 5]
2unique_numbers = set(numbers)
3
4print(unique_numbers)

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:

python
1allowed = set(["read", "write", "delete"])
2
3if "write" in allowed:
4    print("permission granted")

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.

python
1words = ["Apple", "banana", "APPLE", "Banana"]
2normalized = {word.lower() for word in words}
3
4print(normalized)

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:

python
1numbers = [1, 2, 3, 4, 5]
2squares = {n * n for n in numbers}
3
4print(squares)

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
items = [[1, 2], [3, 4]]
result = set(items)

Python raises:

python
TypeError: unhashable type: 'list'

If the elements are lists but you want set behavior, convert each inner list to a tuple first:

python
1items = [[1, 2], [3, 4], [1, 2]]
2result = {tuple(item) for item in items}
3
4print(result)

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:

python
1items = ["a", "b", "a", "c", "b"]
2unique_in_order = list(dict.fromkeys(items))
3
4print(unique_in_order)

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:

python
1result = set()
2
3for value in [1, 2, 2, 3]:
4    result.add(value)
5
6print(result)

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.

Course illustration
Course illustration

All Rights Reserved.