Log In

Python Sets and Python Sets Operations

Python Sets and Python Sets Operations
Reading time: 4 min
Hostman Team
Technical writer

Python sets are a fundamental data type in Python, providing a collection that is unordered, mutable, and does not allow duplicate elements. Sets are powerful tools for various applications, from mathematical operations to database management, due to their unique properties and efficient operations.

Importance of Sets in Programming

Sets are crucial in programming for several reasons:

  1. Uniqueness: Ensures all elements are unique, which is useful for removing duplicates.

  2. Efficiency: Set operations like membership tests (checking if an item is in a set) are optimized, offering average-case O(1) time complexity.

  3. Mathematical Operations: Sets support operations such as union, intersection, and difference, aligning with their mathematical counterparts, making them ideal for tasks like Venn diagrams and common item extraction.

  4. Use Cases: Sets are used in applications such as network analysis, database queries, and data cleaning.

Creating Sets in Python

Creating a set in Python is straightforward. Sets can be created using the set() constructor or by using curly braces {}.

# Using set() constructor
set_a = set([1, 2, 3, 4, 5])

# Using curly braces
set_b = {1, 2, 3, 4, 5}

# Creating an empty set
empty_set = set()

Basic Set Operations: Union, Intersection, Difference

Let's take a look at basic set operations.


The union of two sets is a set containing all elements from both sets, without duplicates.

set_a = {1, 2, 3}
set_b = {3, 4, 5}
union_set = set_a | set_b
# union_set is {1, 2, 3, 4, 5}


The intersection of two sets is a set containing only the elements that are present in both sets.

intersection_set = set_a & set_b
# intersection_set is {3}


The difference of two sets is a set containing elements that are in the first set but not in the second, so it basically substracts elements of one set from another.

difference_set = set_a - set_b
# difference_set is {1, 2}

Advanced Set Operations and Methods

Python sets offer various methods for advanced operations:

Symmetric Difference

The symmetric difference returns a set with elements in either of the sets, but not in both.

sym_diff_set = set_a ^ set_b
# sym_diff_set is {1, 2, 4, 5}

Subset and Superset

Check if a set is a subset or superset of another set.

set_c = {1, 2}
is_subset = set_c <= set_a  # True
is_superset = set_a >= set_c  # True

Updating Sets

Sets can be updated with various methods:

  • add() to add a single element.

  • update() to add multiple elements.

# set_a is now {1, 2, 3, 6}

set_a.update([7, 8])
# set_a is now {1, 2, 3, 6, 7, 8}

Removing Elements

You can use:

  • remove() to remove a specific element (raises KeyError if not found).

  • discard() to remove a specific element (does nothing if not found).

  • pop() to remove and return an arbitrary element.

# set_a is now {1, 2, 3, 7, 8}

# set_a is now {1, 2, 3, 8}

popped_element = set_a.pop()
# popped_element is 1 (example)

Practical Examples of Set Usage

For better understanding, let's see how to use sets in practice.

Removing Duplicates

Sets are an easy way to remove duplicates from a list.

numbers = [1, 2, 2, 3, 4, 4, 5]
unique_numbers = list(set(numbers))
# unique_numbers is [1, 2, 3, 4, 5]

Membership Testing

Sets offer efficient membership testing, ideal for scenarios where this is frequently needed.

names = {"Alice", "Bob", "Charlie"}
is_present = "Bob" in names  # True

Finding Common Items

Sets can be used to find common elements in multiple lists.

list1 = {1, 2, 3}
list2 = {2, 3, 4}
common_elements = list1 & list2
# common_elements is {2, 3}

Performance Considerations with Sets

Sets are highly optimized for certain operations:

  1. Membership Testing: Average-case O(1)* time complexity, making them faster than lists and tuples for this purpose.

  2. Insertion and Deletion: Average-case O(1)* time complexity, allowing for quick modifications.

  3. Memory Usage: Sets typically use more memory than lists for storing the same number of elements due to the need to maintain hash tables for fast access.

* Refers to the average-case time complexity of an operation, which in this context means that the operation takes a constant amount of time on average, regardless of the size of the input. 

However, the actual performance can vary based on the specific application and the size of the data. When choosing data structures, it’s essential to consider the type of operations required and the efficiency of those operations with sets.


Python sets are a versatile and efficient tool for managing collections of unique items and performing various mathematical and logical operations. Their unique properties and methods make them indispensable for many programming tasks. Whether you're removing duplicates, testing for membership, or performing complex mathematical operations, Python sets provide a robust and efficient solution. By understanding and utilizing sets, programmers can enhance the performance and functionality of their code, making sets a vital part of the Python programming toolkit.