What Is a Set in Python?

A set in Python is a collection of unique values. This page helps beginners understand what a set is, when to use it, and how it is different from other collection types.

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

colors = set(["red", "blue", "red"])
print(colors)

What this does:

  • The first line creates a set with duplicate 3, but the duplicate is removed.
  • The second example uses set() to convert a list into a set.
  • In both cases, only unique values remain.

Possible output:

{1, 2, 3}
{'red', 'blue'}

Use curly braces with values or the set() function to create a set. Duplicate values are removed automatically.

Set definition

A set is a built-in Python collection type.

A set:

  • stores multiple values in one variable
  • keeps only unique values
  • is unordered, so items do not have a fixed position
  • is useful when you care about membership or removing duplicates

Example:

fruits = {"apple", "banana", "apple"}
print(fruits)

Output:

{'apple', 'banana'}

Even though "apple" appears twice, the set keeps it only once.

How a set is different from a list or tuple

Sets are different from what is a list in Python and what is a tuple in Python.

Here are the main differences:

  • Lists keep order and allow duplicates
  • Tuples keep order and are often used for fixed data
  • Sets do not keep duplicates
  • Sets do not support indexing like my_set[0]

Example:

my_list = [1, 2, 2, 3]
my_tuple = (1, 2, 2, 3)
my_set = {1, 2, 2, 3}

print(my_list)
print(my_tuple)
print(my_set)

Possible output:

[1, 2, 2, 3]
(1, 2, 2, 3)
{1, 2, 3}

The set removes the duplicate 2.

How to create a set

You can create a set in two common ways.

Use curly braces

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

Use set()

This is useful when converting another iterable, such as a list or string, into a set.

letters = set(["a", "b", "a"])
print(letters)

Output:

{'a', 'b'}

Create an empty set

Use set() for an empty set.

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

Do not use {} if you want an empty set.

empty_value = {}
print(type(empty_value))

Output:

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

{} creates an empty what is a dictionary in Python, not an empty set.

If you want more detail, see creating a set in Python.

What sets are good for

Sets are useful when uniqueness matters.

Common uses:

  • removing duplicate values from a list
  • checking if a value exists quickly
  • comparing groups of values
  • finding shared or different items between collections

Example: remove duplicates from a list

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

print(unique_numbers)

Possible output:

{1, 2, 3}

If you want a step-by-step version of this task, see how to remove duplicates from a list in Python.

Example: check membership

colors = {"red", "blue", "green"}

print("red" in colors)
print("yellow" in colors)

Output:

True
False

Basic things you can do with a set

Python sets come with useful methods.

Add an item with add()

colors = {"red", "blue"}
colors.add("green")

print(colors)

See also: Python set add() method

Add many items with update()

colors = {"red", "blue"}
colors.update(["green", "yellow"])

print(colors)

Remove items with remove() or discard()

colors = {"red", "blue", "green"}

colors.remove("blue")
print(colors)

colors.discard("yellow")  # no error if item is missing
print(colors)

remove() causes an error if the item is not in the set.

discard() does not.

See also: Python set remove() method

Combine sets with union()

set1 = {1, 2, 3}
set2 = {3, 4, 5}

result = set1.union(set2)
print(result)

Possible output:

{1, 2, 3, 4, 5}

See also: Python set union() method

Find shared values with intersection()

set1 = {1, 2, 3}
set2 = {2, 3, 4}

result = set1.intersection(set2)
print(result)

Output:

{2, 3}

See also: Python set intersection() method

Find differences with difference()

set1 = {1, 2, 3}
set2 = {2, 3, 4}

result = set1.difference(set2)
print(result)

Output:

{1}

Important beginner rules

When working with sets, remember these rules:

  • Set items must be hashable, so lists cannot be set items
  • The printed order of a set may look random
  • You cannot access set items by index
  • If you need ordered data, use a list or tuple instead

Example of invalid code:

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

This causes an error because lists are mutable and cannot be stored inside a set.

A tuple can be used instead:

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

Common mistakes

Beginners often run into these problems:

  • using {} and expecting an empty set
  • trying to access a set item by index
  • expecting duplicates to stay in the set
  • trying to put a list inside a set

Example of indexing error:

colors = {"red", "blue", "green"}
print(colors[0])

This fails because sets do not have positions like lists.

If you are not sure what a variable contains, these checks can help:

print(my_set)
print(type(my_set))
print(len(my_set))
print('red' in my_set)

These are useful for debugging because they show:

  • the actual values in the set
  • whether the variable is really a set
  • how many unique items it contains
  • whether a specific value exists in it

FAQ

Does a set keep items in order?

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

Can a set contain duplicate values?

No. Duplicate values are removed automatically.

How do I create an empty set in Python?

Use set(). If you use {}, Python creates an empty dictionary.

When should I use a set instead of a list?

Use a set when you need unique values or fast membership checks.

See also