Python set() Function Explained

The built-in set() function creates a set in Python. A set is a collection that stores unique values only.

Beginners often use set() to:

  • remove duplicates from a list
  • check whether a value exists
  • work with set operations like union and intersection

Quick example

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

Output:

{1, 2, 3}

Use set() to create a set from an iterable. A set keeps only unique values.

What set() does

set() creates a new set object.

Important things to know:

  • A set stores unique items only
  • Duplicate values are removed automatically
  • Sets are unordered, so item order is not guaranteed
  • A very common use is removing duplicates from a list or another iterable

Example:

colors = ["red", "blue", "red", "green"]
unique_colors = set(colors)

print(unique_colors)

Possible output:

{'blue', 'green', 'red'}

Notice that "red" appears only once.

If you want a broader beginner introduction to sets, see Python sets explained.

Basic syntax

The syntax is:

set(iterable)

The argument is optional.

  • If you pass an iterable, Python creates a set from its items
  • If you pass nothing, Python creates an empty set

Common iterables you can use with set():

  • list
  • tuple
  • string
  • range()
  • another set

Examples:

print(set([1, 2, 2, 3]))
print(set((10, 20, 10)))
print(set("hello"))
print(set(range(4)))

Possible output:

{1, 2, 3}
{10, 20}
{'h', 'e', 'l', 'o'}
{0, 1, 2, 3}

If you are comparing built-in constructors, you may also want to read Python list() function explained.

Creating an empty set correctly

To create an empty set, use:

empty_set = set()
print(empty_set)
print(type(empty_set))

Output:

set()
<class 'set'>

This is important because:

  • set() creates an empty set
  • {} creates an empty dictionary, not a set

Example:

a = set()
b = {}

print(type(a))
print(type(b))

Output:

<class 'set'>
<class 'dict'>

This is one of the most common beginner mistakes.

For more on creating sets, see Python set: creating a set.

Using set() with common iterables

From a list

This is one of the most common uses of set().

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

print(result)

Output:

{1, 2, 3}

This is useful when you need unique values. For a task-focused example, see how to remove duplicates from a list in Python.

From a tuple

values = (5, 5, 10, 15)
result = set(values)

print(result)

Output:

{5, 10, 15}

From a string

When you use set() on a string, Python treats the string as an iterable of characters.

text = "banana"
result = set(text)

print(result)

Possible output:

{'b', 'a', 'n'}

The result contains unique characters only.

From range()

result = set(range(5))
print(result)

Output:

{0, 1, 2, 3, 4}

What kinds of values a set can contain

Set items must be hashable.

In simple terms, this means the value must be a type Python can safely use inside a set.

These usually work:

  • int
  • str
  • float
  • tuple (if its contents are also hashable)

Example:

items = {1, "hello", (10, 20)}
print(items)

These do not work inside a set:

  • list
  • dict
  • set

Example that causes an error:

bad_set = {[1, 2], [3, 4]}

Python raises a TypeError because lists are mutable and cannot be added to a set.

A safer example:

good_set = {(1, 2), (3, 4)}
print(good_set)

Output:

{(1, 2), (3, 4)}

When to use set()

Use set() when:

  • you need unique values only
  • you want fast membership checks with in
  • order does not matter
  • you need set operations like union or intersection

Example of membership checking:

allowed_ids = set([101, 102, 103])

print(102 in allowed_ids)
print(999 in allowed_ids)

Output:

True
False

This is a common real-world use of sets.

set() vs {}

These forms are similar, but they do not mean the same thing.

Use set() for an empty set

empty_set = set()
print(type(empty_set))

Output:

<class 'set'>

Use {1, 2, 3} for a non-empty set literal

numbers = {1, 2, 3}
print(type(numbers))

Output:

<class 'set'>

Use {} for an empty dictionary

empty_dict = {}
print(type(empty_dict))

Output:

<class 'dict'>

So the rule is simple:

  • set() → empty set
  • {1, 2, 3} → non-empty set
  • {} → empty dictionary

Common mistakes

These are common problems beginners run into with set().

Using {} and expecting an empty set

This creates a dictionary, not a set:

value = {}
print(type(value))

Passing a non-iterable value like set(5)

set() expects one iterable argument. An integer is not iterable.

set(5)

This raises a TypeError. If you are seeing a related message, see TypeError: int object is not iterable fix.

Trying to put lists or dictionaries inside a set

This does not work because lists and dictionaries are unhashable.

value = {[1, 2], [3, 4]}

Expecting set() to keep the original order

Sets are unordered. If you print a set, the items may not appear in the same order as the original iterable.

Confusing set() with list() or dict()

These built-in functions create different types of objects. Choose the one that matches your goal.

Useful debugging checks

If something is not working, these quick checks can help:

print(type(my_value))
print(my_value)
print(list(my_set))
print(len(my_set))

What they help you see:

  • print(type(my_value)) checks the actual type
  • print(my_value) shows the current value
  • print(list(my_set)) converts the set to a list so it is easier to inspect
  • print(len(my_set)) shows how many unique items are in the set

FAQ

Does set() remove duplicates?

Yes. Duplicate values are automatically removed when the set is created.

How do I make an empty set in Python?

Use set(). Do not use {}, because that creates an empty dictionary.

Does set() keep the original order?

No. A set is unordered, so you should not rely on item order.

Can I use set() on a string?

Yes. It creates a set of unique characters from the string.

Example:

print(set("apple"))

Possible output:

{'a', 'p', 'l', 'e'}

Why does set([1, 2], [3, 4]) not work?

set() takes one optional iterable argument, not multiple positional values.

If you want all those values in one set, combine them into a single iterable first:

print(set([1, 2, 3, 4]))

See also