codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐍

Python Programming

22 / 68 topics
19Python Lists20Python Tuples21Python Dictionaries22Python Sets23Python Arrays
Tutorials/Python Programming/Python Sets
🐍Python Programming

Python Sets

Updated 2026-05-15
20 min read

Python Sets

Sets are a fundamental data structure in Python that store collections of unique elements. Unlike lists or tuples, sets automatically eliminate any duplicate values. This makes them particularly useful when you need to ensure all items in a collection are distinct. Additionally, sets support efficient mathematical operations like union, intersection, difference, and symmetric difference.

In this tutorial, we'll explore how to create sets, understand their properties, perform various set operations, and learn about the frozenset type and set comprehensions.

Introduction

Sets in Python are unordered collections of unique elements. They are defined using curly braces {} or by using the set() constructor. Sets are mutable, meaning you can add or remove items after their creation. However, since sets are unordered, they do not support indexing or slicing like lists or tuples.

Understanding sets is crucial for tasks that require uniqueness and efficient set operations, making them a valuable tool in your Python programming toolkit.

Creating Sets

Using Curly Braces

You can create a set by enclosing elements within curly braces. Each element must be separated by a comma.

create_set.py
1# Create a set using curly braces
2fruits = {"apple", "banana", "cherry"}
3print(fruits)
Output
{'banana', 'apple', 'cherry'}

Using the set() Constructor

You can also create a set using the set() constructor. This is particularly useful when converting other iterable types like lists or tuples into sets.

set_constructor.py
1# Create a set from a list
2numbers = [1, 2, 3, 4, 5]
3unique_numbers = set(numbers)
4print(unique_numbers)
Output
{1, 2, 3, 4, 5}

Empty Sets

To create an empty set, you must use the set() constructor. Using curly braces {} creates an empty dictionary, not a set.

empty_set.py
1# Creating an empty set using set()
2empty_set = set()
3print(type(empty_set))
4
5# This will create an empty dictionary, not a set
6empty_dict = {}
7print(type(empty_dict))
Output
<class 'set'>
<class 'dict'>

Uniqueness

One of the most significant properties of sets is that they automatically eliminate duplicate values. When you add elements to a set, any duplicates are ignored.

uniqueness.py
1# Duplicates are automatically removed
2fruits = {"apple", "banana", "cherry", "apple"}
3print(fruits)
Output
{'banana', 'apple', 'cherry'}

Set Operations

Sets support several mathematical operations, which can be performed using methods or operators.

Union

The union of two sets is a set containing all elements from both sets. You can use the union() method or the | operator to find the union.

set_union.py
1# Using union() method
2set1 = {1, 2, 3}
3set2 = {3, 4, 5}
4union_set = set1.union(set2)
5print(union_set)
6
7# Using | operator
8union_operator_set = set1 | set2
9print(union_operator_set)
Output
{1, 2, 3, 4, 5}
{1, 2, 3, 4, 5}

Intersection

The intersection of two sets is a set containing only the elements that are common to both sets. You can use the intersection() method or the & operator to find the intersection.

set_intersection.py
1# Using intersection() method
2set1 = {1, 2, 3}
3set2 = {3, 4, 5}
4intersection_set = set1.intersection(set2)
5print(intersection_set)
6
7# Using & operator
8intersection_operator_set = set1 & set2
9print(intersection_operator_set)
Output
{3}
{3}

Difference

The difference of two sets is a set containing elements that are in the first set but not in the second. You can use the difference() method or the - operator to find the difference.

set_difference.py
1# Using difference() method
2set1 = {1, 2, 3}
3set2 = {3, 4, 5}
4difference_set = set1.difference(set2)
5print(difference_set)
6
7# Using - operator
8difference_operator_set = set1 - set2
9print(difference_operator_set)
Output
{1, 2}
{1, 2}

Symmetric Difference

The symmetric difference of two sets is a set containing elements that are in either of the sets but not in their intersection. You can use the symmetric_difference() method or the ^ operator to find the symmetric difference.

set_symmetric_difference.py
1# Using symmetric_difference() method
2set1 = {1, 2, 3}
3set2 = {3, 4, 5}
4symmetric_difference_set = set1.symmetric_difference(set2)
5print(symmetric_difference_set)
6
7# Using ^ operator
8symmetric_difference_operator_set = set1 ^ set2
9print(symmetric_difference_operator_set)
Output
{1, 2, 4, 5}
{1, 2, 4, 5}

frozenset

A frozenset is an immutable version of a set. Once created, you cannot add or remove elements from it. This makes frozensets hashable and suitable for use as dictionary keys or elements of other sets.

frozenset_example.py
1# Creating a frozenset
2immutable_set = frozenset([1, 2, 3])
3print(immutable_set)
4
5# Trying to modify a frozenset will raise an error
6try:
7 immutable_set.add(4)
8except AttributeError as e:
9 print(e)
Output
frozenset({1, 2, 3})
'frozenset' object has no attribute 'add'

Set Comprehensions

Set comprehensions provide a concise way to create sets. They are similar to list comprehensions but use curly braces {}.

set_comprehension.py
1# Using set comprehension to create a set of squares
2squares = {x**2 for x in range(10)}
3print(squares)
Output
{0, 1, 4, 9, 16, 25, 36, 49, 64, 81}

Practical Example

Let's create a practical example that demonstrates the use of sets in a real-world scenario. We'll write a program to find common elements between two lists and unique elements in each list.

practical_example.py
1# Define two lists
2list1 = [1, 2, 3, 4, 5]
3list2 = [4, 5, 6, 7, 8]
4
5# Convert lists to sets
6set1 = set(list1)
7set2 = set(list2)
8
9# Find common elements (intersection)
10common_elements = set1.intersection(set2)
11print(f"Common elements: {common_elements}")
12
13# Find unique elements in each list (difference)
14unique_in_list1 = set1.difference(set2)
15unique_in_list2 = set2.difference(set1)
16print(f"Unique in list1: {unique_in_list1}")
17print(f"Unique in list2: {unique_in_list2}")
Output
Common elements: {4, 5}
Unique in list1: {1, 2, 3}
Unique in list2: {8, 6, 7}

Summary

  • Sets are unordered collections of unique elements.
  • They support efficient mathematical operations like union, intersection, difference, and symmetric difference.
  • Sets can be created using curly braces or the set() constructor.
  • The frozenset type provides an immutable version of sets.
  • Set comprehensions offer a concise way to create sets.
OperationMethodOperator
Unionunion()`
Intersectionintersection()&
Differencedifference()-
Symmetric Differencesymmetric_difference()^

What's Next?

In the next tutorial, we'll explore Python Arrays, which are a more efficient way to store large arrays of numerical data compared to lists. Understanding arrays will be beneficial for tasks involving numerical computations and data analysis.

Stay tuned!


PreviousPython DictionariesNext Python Arrays

Recommended Gear

Python DictionariesPython Arrays