This article is part of in the series
Published: Wednesday 26th March 2025

set operations python tutorial

If you are working with a variety of set operations such as union, intersection, difference, and symmetric difference, then you will be working with unique elements a lot. Sets in Python are a powerful data structure that allows for efficient handling of unique elements. Set operations are essential for mathematical computations, database operations, and data analysis.

Today at PythonCentral, we will explain Python set operations, their practical applications, and efficient ways to use them. Get. Set. Learn!

What is Python Set Operations

Before we move to operations, let us understand what a set is. A set is an unordered collection of unique elements in Python. They are defined using "{}" or the "set()" constructor. Here is an example syntax to show how to create a set in Python:

set_a = {1, 2, 3, 4}
set_b = {3, 4, 5, 6}
print(set_a) # Output: {1, 2, 3, 4}

What are Some of the Common Set Operations in Python

Today, let us learn 4 common set operations in Python:

  • Union
  • Intersection
  • Difference
  • Symmetric difference

Union

The operator for union is "|" or "union()". This operation combines elements from both sets, removing duplicates.

union_set = set_a | set_b # This uses | operator
print(union_set)

union_set = set_a.union(set_b) # This uses the union() method
print(union_set)

Intersection

The intersection operator is "&" or "intersection()". This operation finds common elements between sets.

intersection_set = set_a & set_b # This uses the & operator
print(intersection_set)

intersection_set = set_a.intersection(set_b) # This uses the intersection() method
print(intersection_set)

Difference

The operator for difference is "-" or "difference()". This operation finds elements in one set that are not in another.

diff_set = set_a - set_b # This uses - operator
print(diff_set)

diff_set = set_a.difference(set_b) # This uses the difference() method
print(diff_set)

Symmetric Difference

The symmetric difference operator is "^" or "symmetric_difference()". This symmetric difference operation finds elements that are in either set, but not in both.

sym_diff_set = set_a ^ set_b # This uses ^ operator
print(sym_diff_set)

sym_diff_set = set_a.symmetric_difference(set_b) # This uses symmetric_difference() method
print(sym_diff_set)

How to Check Subsets and Supersets

Here is how you can check subsets or supersets:

  • issubset(): Checks if all elements of one set exist in another.
  • issuperset(): Checks if a set contains all elements of another set.

Here are the sample syntaxes for subsets and supersets:

print(set_a.issubset(set_b))
print(set_a.issuperset(set_b))

How to Modify Sets

Now that we have learnt the basics of set operations in Python, let us see how to modify sets.

Adding and Removing Elements

Here is how you can add or remove elements:

set_a.add(10) # Adds a single element
set_a.remove(3) # Removes an element (raises error if not found)
set_a.discard(5) # Removes an element (no error if not found)
set_a.pop() # Removes a random element
print(set_a)

Clearing a Set

An example syntax to clear a set:

set_a.clear() # Removes all elements
print(set_a) # Output: set()

Real-world Applications of Set Operations

What is the fun in learning something when we do not know where it will be helpful for us? Here are some practical applications of set operations in Python.

How to Remove Duplicates from a List

With Python, you can use operators to remove duplicates from a list.

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

Finding Common Elements Between Two Lists

Here is how you can find common elements when you work with two different lists.

list1 = ["python", "central", "rocks"]
list2 = ["linux", "rocks", "python"]
common_elements = set(list1) & set(list2)
print(common_elements) # Output: {"python"}

How to Find Elements in One List but Not in Another

Here is how you can cherry pick the unique elements in one list:

difference = set(list1) - set(list2)
print(difference)

Checking for Unique Characters in a String

In the previous example, we learnt how to find a unique element in one list. Now, let us learn how to check for unique characters in a string.

text = "hello world"
unique_chars = set(text)
print(unique_chars) # Output: {' ', 'h', 'e', 'l', 'o', 'w', 'r', 'd'}

Wrapping Up

Python set operations gives you a fast and efficient way to manipulate unique data. Whether you're handling mathematical operations, filtering data, or improving search operations, sets are a fundamental tool in Python programming.

Learning this can help you with data structures, improve efficiency, and solve complex problems effortlessly. Before we wrap up, you need to keep two things in mind:

Set operations are generally faster than lists for membership tests ("O(1)" on average vs. "O(n)" for lists).
Union, intersection, and difference operations run in "O(n+m)" time complexity, making them efficient for large datasets.

Related Articles