Python Sets: A Simple Guide for Beginners

wordpediax
4 min readNov 4, 2023

Python offers a wide range of data structures, and sets are one of the fundamental and versatile options(After Python List & Tuple). Sets are used to store collections of unique elements.

In this guide, we will explore Python sets from the ground up, understand what they are, how to create and manipulate them and provide you with practical examples to get you started.

What Are Sets?

In Python, a set is an unordered collection of unique elements. This means that, unlike lists or tuples, sets do not allow duplicate values. Sets are defined by enclosing elements in curly braces {} or using the set() constructor.

Here’s how you can create a simple set in Python:

my_set = {1, 2, 3}

In this example, my_set is a Python set containing three unique integers.

Creating Sets

Sets can be created in several ways:

1. Using Curly Braces

The most common way to create a set is by enclosing elements within curly braces.

my_set = {1, 2, 3}

2. Using the set() Constructor

You can also create a set using the set() constructor, which can convert other iterable objects like lists, tuples, or strings into sets.

# Using a list
my_list = [4, 5, 6]
my_set = set(my_list)

# Using a string
my_string = "Hello"
my_set = set(my_string)

3. Creating an Empty Set

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

empty_set = set()

Accessing Set Elements

Unlike lists, sets are unordered, which means they do not have indices. Therefore, you cannot access set elements by index. However, you can use loops to iterate over the elements of a set.

my_set = {1, 2, 3, 4, 5}

for element in my_set:
print(element)

This loop will iterate over the elements of the set in an arbitrary order.

Modifying Sets

Sets are mutable, which means you can add, remove, or update elements within a set. Here are some common set operations:

Adding Elements

You can add elements to a set using the add() method.

my_set = {1, 2, 3}
my_set.add(4) # Add 4 to the set

Removing Elements

To remove an element from a set, you can use the remove() or discard() method. The difference between the two is that remove() will raise an error if the element is not found, while discard() will not.

my_set = {1, 2, 3, 4, 5}
my_set.remove(4) # Remove 4 from the set
my_set.discard(6) # Try to remove 6 (no error if not found)

Updating Sets

You can update a set with elements from another set using methods like update() or perform set operations like union, intersection, and difference.

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

set1.update(set2) # Add elements from set2 to set1
# Resulting set1: {1, 2, 3, 4, 5}

union_set = set1.union(set2) # Create a union of set1 and set2
# Resulting union_set: {1, 2, 3, 4, 5}

intersection_set = set1.intersection(set2) # Create an intersection of set1 and set2
# Resulting intersection_set: {3}

difference_set = set1.difference(set2) # Find the difference between set1 and set2
# Resulting difference_set: {1, 2}

Set Methods

Sets come with a variety of methods for different operations.

Let’s explore some of the most commonly used set methods.

add()

The add() method allows you to add an element to the set. If the element already exists, it won't be added again.

Example:

fruits = {"apple", "banana", "cherry"}
fruits.add("orange")
# Resulting fruits: {"apple", "banana", "cherry", "orange"}

remove()

The remove() method is used to remove a specific element from the set. If the element does not exist, it will raise an error.

Example:

fruits = {"apple", "banana", "cherry"}
fruits.remove("banana")
# Resulting fruits: {"apple", "cherry"}

discard()

The discard() method is similar to remove(), but it won't raise an error if the element does not exist.

Example:

fruits = {"apple", "banana", "cherry"}
fruits.discard("orange")
# Resulting fruits: {"apple", "banana", "cherry"}

clear()

The clear() method removes all elements from the set, making it empty.

Example:

fruits = {"apple", "banana", "cherry"}
fruits.clear()
# Resulting fruits: set()

update()

The update() method allows you to add elements from another set (or any iterable) to the current set.

Example:

fruits = {"apple", "banana", "cherry"}
new_fruits = {"orange", "kiwi"}
fruits.update(new_fruits)
# Resulting fruits: {"apple", "banana", "cherry", "orange", "kiwi"}

Conclusion

Python sets are a powerful data structure for storing unique elements and performing set operations efficiently. By mastering the creation and manipulation of sets, you can make your Python code more elegant and efficient.

Whether you’re removing duplicates from a list, checking for common elements, or performing mathematical set operations, sets are a valuable addition to your Python programming toolkit.

--

--

wordpediax

I like to read and write articles on tech topics like Python, ML, AI. Beside this, I like to thread life into words and connect myself with nature.