Python Sets

Last Updated 12 Sep, 2026

A set in Python is an unordered collection of unique elements. Unlike a list or tuple, a set does not store duplicate values. Sets are useful when you need to keep only distinct items or perform operations such as union, intersection, difference, and symmetric difference.

A set is written using curly braces {}.

numbers = {10, 20, 30, 40}

print(numbers)

The order of elements in a set is not guaranteed.

What is a Set?

A set is a built-in Python data type that stores multiple values without duplicates. Set elements must be hashable, so mutable types such as lists and dictionaries cannot be directly stored in a set.

A set can contain different immutable data types:

data = {10, "Python", 25.5, True}

print(data)

Since sets are unordered, you should not rely on a particular order when displaying or processing their elements.

Creating Sets

The simplest way to create a set is to place elements inside curly braces:

fruits = {"apple", "banana", "cherry"}

print(fruits)

An empty set needs a little care. {} creates an empty dictionary, not a set.

myset = set()

print(type(myset))

Output:

<class 'set'>

You can also create a set from another iterable using the set() function:

numbers = set([10, 20, 30, 40])

print(numbers)

Strings can also be converted into sets:

letters = set("Python")

print(letters)

The resulting set contains the unique characters from the string.

Duplicate Elements

One of the main properties of a Python set is that it automatically removes duplicate elements.

numbers = {10, 20, 10, 30, 20, 40}

print(numbers)

Output will contain each value only once:

{10, 20, 30, 40}

This makes sets useful for removing duplicate values from a list.

numbers = [10, 20, 10, 30, 20, 40]

unique_numbers = set(numbers)

print(unique_numbers)

Accessing Elements

Sets do not support indexing because they are unordered collections.

So, this is not valid:

numbers = {10, 20, 30}

print(numbers[0])

Instead, you can use a for loop to access the elements:

fruits = {"apple", "banana", "cherry"}

for fruit in fruits:
    print(fruit)

You can also check whether an element exists using the in operator:

fruits = {"apple", "banana", "cherry"}

print("apple" in fruits)

Output:

True

Adding and Removing Elements

The add() method adds a single element to a set.

fruits = {"apple", "banana"}

fruits.add("cherry")

print(fruits)

To add multiple elements, use update():

fruits = {"apple", "banana"}

fruits.update(["cherry", "orange"])

print(fruits)

Python provides several ways to remove elements from a set.

remove()

The remove() method deletes a specified element. It raises a KeyError if the element does not exist.

fruits = {"apple", "banana", "cherry"}

fruits.remove("banana")

print(fruits)

discard()

discard() also removes an element, but it does not raise an error if the element is missing.

fruits = {"apple", "banana", "cherry"}

fruits.discard("orange")

print(fruits)

pop()

The pop() method removes and returns an arbitrary element from the set.

numbers = {10, 20, 30}

item = numbers.pop()

print(item)
print(numbers)

Because sets are unordered, you should not expect a particular element to be removed.

To remove all elements, use clear():

numbers = {10, 20, 30}

numbers.clear()

print(numbers)

Common Set Methods

Python provides several methods for working with sets.

Some commonly used methods are:

  • add() – adds an element 
  • update() – adds multiple elements 
  • remove() – removes an element 
  • discard() – removes an element without raising an error if missing 
  • pop() – removes an arbitrary element 
  • clear() – removes all elements 
  • copy() – creates a copy of a set 
  • union() – combines sets 
  • intersection() – finds common elements 
  • difference() – finds elements present in one set but not another 
  • symmetric_difference() – finds elements that are not common between two sets 

For example:

numbers = {10, 20, 30}

new_numbers = numbers.copy()

print(new_numbers)

Set methods can also be used to compare collections without manually looping through their elements.

Union of Sets

Union combines the elements of two or more sets. Duplicate values appear only once.

The union() method can be used for this operation:

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

result = set1.union(set2)

print(result)

Output:

{1, 2, 3, 4, 5}

You can also use the | operator:

result = set1 | set2

print(result)

Intersection of Sets

Intersection returns the elements that are present in both sets.

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

result = set1.intersection(set2)

print(result)

Output:

{3, 4}

The & operator can also be used:

result = set1 & set2

print(result)

Difference of Sets

The difference operation returns elements that are present in the first set but not in the second set.

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

result = set1.difference(set2)

print(result)

Output:

{1, 2}

You can also use the - operator:

result = set1 - set2

print(result)

The order matters here. set1 - set2 and set2 - set1 can produce different results.

Symmetric Difference

Symmetric difference returns the elements that are present in either set, but not in both.

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

result = set1.symmetric_difference(set2)

print(result)

Output:

{1, 2, 5, 6}

The ^ operator provides the same result:

result = set1 ^ set2

print(result)

Here, 3 and 4 are excluded because they occur in both sets.

Frozenset in Python

A frozenset is an immutable version of a Python set. Once a frozenset is created, its elements cannot be added or removed.

You can create one using the frozenset() function:

numbers = frozenset([10, 20, 30, 40])

print(numbers)

Unlike a normal set, a frozenset does not provide methods such as add(), remove(), or clear() because its contents cannot be changed.

numbers = frozenset([10, 20, 30])

numbers.add(40)

This raises an AttributeError.

Frozensets still support set operations such as union, intersection, difference, and symmetric difference:

set1 = frozenset([1, 2, 3])
set2 = frozenset([3, 4, 5])

print(set1.union(set2))
print(set1.intersection(set2))

Frozensets are useful when you need a set-like collection that should remain unchanged.