Work with Sets in Python

PythonBeginner
Practice Now

Introduction

In this lab, you will gain hands-on experience with sets in Python. Sets are a fundamental data structure used for storing unique, unordered elements. They are highly efficient for tasks like checking if an element is present in a collection and for performing mathematical set operations.

You will learn how to create sets, add and remove elements, and perform common operations like union, intersection, and difference. Finally, you will see a practical application of sets by using them to easily remove duplicate items from a list.

Create and Add Elements to a Set

In this first step, you will learn how to create sets and add new elements to them. Sets are collections of unique items, meaning they automatically discard any duplicates.

Your environment includes an empty file named set_basics.py. Using the file explorer on the left side of the editor, find and open ~/project/set_basics.py.

Add the following Python code to the file. This code demonstrates several ways to create a set.

## Method 1: Using curly braces {}
## This creates a set with initial elements.
my_set = {'apple', 'banana', 'cherry'}
print("Set created with braces:", my_set)
print("Type of my_set:", type(my_set))

## Note: Sets automatically remove duplicate elements.
duplicate_set = {'apple', 'banana', 'apple'}
print("Set with duplicates:", duplicate_set)

## Method 2: Using the set() constructor on an iterable (like a string)
## This creates a set from the unique characters in the string.
char_set = set('hello world')
print("Set from a string:", char_set)

## Method 3: Creating an empty set
## You must use set() to create an empty set. {} creates an empty dictionary.
empty_set = set()
print("An empty set:", empty_set)
print("Type of empty_set:", type(empty_set))

Save the file. Now, open a terminal in your editor (you can use the menu: Terminal -> New Terminal) and run your script with the following command.

python ~/project/set_basics.py

You will see output similar to the following. Notice that the order of elements in a set is not guaranteed, and duplicates are removed.

Set created with braces: {'cherry', 'apple', 'banana'}
Type of my_set: <class 'set'>
Set with duplicates: {'banana', 'apple'}
Set from a string: {'d', 'l', 'o', 'r', 'w', ' ', 'h', 'e'}
An empty set: set()
Type of empty_set: <class 'set'>

Next, let's add new elements to an existing set. You can add a single element with the add() method or multiple elements with the update() method.

Add the following code to the bottom of your set_basics.py file.

## --- Adding elements ---
fruits = {'apple', 'banana'}
print("\nOriginal fruits set:", fruits)

## Use add() to add a single element
fruits.add('orange')
print("After adding 'orange':", fruits)

## add() has no effect if the element is already present
fruits.add('apple')
print("After adding 'apple' again:", fruits)

## Use update() to add multiple elements from an iterable (like a list)
fruits.update(['mango', 'grape'])
print("After updating with a list:", fruits)

Save the file again and run the updated script from the terminal.

python ~/project/set_basics.py

The output will now include the results of adding elements.

Set created with braces: {'cherry', 'apple', 'banana'}
Type of my_set: <class 'set'>
Set with duplicates: {'banana', 'apple'}
Set from a string: {'d', 'l', 'o', 'r', 'w', ' ', 'h', 'e'}
An empty set: set()
Type of empty_set: <class 'set'>

Original fruits set: {'banana', 'apple'}
After adding 'orange': {'banana', 'orange', 'apple'}
After adding 'apple' again: {'banana', 'orange', 'apple'}
After updating with a list: {'grape', 'mango', 'banana', 'orange', 'apple'}

You have now learned how to create sets and modify them by adding new elements.

Remove Elements from a Set

In this step, you will learn various ways to remove elements from a set. Since sets are unordered, you cannot use an index to remove items. Instead, Python provides specific methods for this purpose.

Find and open the file set_removal.py in your ~/project directory.

Add the following code to the file. It demonstrates the remove(), discard(), pop(), and clear() methods.

## --- Removing elements ---
my_set = {'a', 'b', 'c', 'd', 'e'}
print("Original set:", my_set)

## Method 1: remove()
## This removes a specified element. It raises a KeyError if the element is not found.
my_set.remove('b')
print("After removing 'b':", my_set)
## The following line would cause an error: my_set.remove('z')

## Method 2: discard()
## This also removes a specified element, but it does NOT raise an error if the element is not found.
print("\nStarting set for discard:", my_set)
my_set.discard('c')
print("After discarding 'c':", my_set)
my_set.discard('z') ## 'z' is not in the set, but no error occurs.
print("After discarding 'z' (non-existent):", my_set)

## Method 3: pop()
## This removes and returns an arbitrary element from the set.
## Since sets are unordered, you don't know which item will be popped.
print("\nStarting set for pop:", my_set)
popped_item = my_set.pop()
print("Popped item:", popped_item)
print("Set after pop():", my_set)

## Method 4: clear()
## This removes all elements from the set, leaving an empty set.
print("\nStarting set for clear:", my_set)
my_set.clear()
print("Set after clear():", my_set)

Save the file. Now, run the script from your terminal.

python ~/project/set_removal.py

Your output should look similar to this. The element removed by pop() may be different each time you run the script because sets are unordered.

Original set: {'d', 'c', 'e', 'a', 'b'}
After removing 'b': {'d', 'c', 'e', 'a'}

Starting set for discard: {'d', 'c', 'e', 'a'}
After discarding 'c': {'d', 'e', 'a'}
After discarding 'z' (non-existent): {'d', 'e', 'a'}

Starting set for pop: {'d', 'e', 'a'}
Popped item: d
Set after pop(): {'e', 'a'}

Starting set for clear: {'e', 'a'}
Set after clear(): set()

You now know the key methods for removing elements from a set and understand the important difference between remove() and discard().

Perform Set Operations

Sets are particularly powerful for performing mathematical operations like union, intersection, and difference. In this step, you will learn how to perform these operations in Python.

Find and open the file set_operations.py in your ~/project directory.

Add the following code to the file. This code defines two sets and then performs the three main set operations on them.

set_a = {'a', 'b', 'c', 'd'}
set_b = {'c', 'd', 'e', 'f'}

print("Set A:", set_a)
print("Set B:", set_b)

## --- Union ---
## The union contains all unique elements from both sets.
## You can use the | operator or the .union() method.
union_set_op = set_a | set_b
union_set_method = set_a.union(set_b)
print("\nUnion with | operator:", union_set_op)
print("Union with .union() method:", union_set_method)

## --- Intersection ---
## The intersection contains only the elements that are common to both sets.
## You can use the & operator or the .intersection() method.
intersection_set_op = set_a & set_b
intersection_set_method = set_a.intersection(set_b)
print("\nIntersection with & operator:", intersection_set_op)
print("Intersection with .intersection() method:", intersection_set_method)

## --- Difference ---
## The difference contains elements that are in the first set but NOT in the second set.
## You can use the - operator or the .difference() method.
difference_set_op = set_a - set_b
difference_set_method = set_a.difference(set_b)
print("\nDifference (A - B) with - operator:", difference_set_op)
print("Difference (A - B) with .difference() method:", difference_set_method)

## Note that the order matters for difference
difference_b_a = set_b - set_a
print("Difference (B - A):", difference_b_a)

Save the file and execute it from the terminal.

python ~/project/set_operations.py

The output will clearly show the results of each operation.

Set A: {'d', 'c', 'a', 'b'}
Set B: {'d', 'c', 'f', 'e'}

Union with | operator: {'d', 'c', 'f', 'e', 'a', 'b'}
Union with .union() method: {'d', 'c', 'f', 'e', 'a', 'b'}

Intersection with & operator: {'d', 'c'}
Intersection with .intersection() method: {'d', 'c'}

Difference (A - B) with - operator: {'a', 'b'}
Difference (A - B) with .difference() method: {'a', 'b'}
Difference (B - A): {'f', 'e'}

You have successfully used both operator symbols and methods to perform union, intersection, and difference operations on sets.

Use a Set to Remove Duplicates from a List

One of the most common and practical uses for sets is to quickly remove duplicate elements from a list. Because sets can only contain unique elements, converting a list to a set and back to a list is a simple and efficient way to achieve this.

Find and open the final file for this lab, remove_duplicates.py, in your ~/project directory.

Add the following code to the file.

## A list containing several duplicate numbers
numbers_list = [1, 5, 2, 3, 5, 1, 4, 2, 2, 5]
print("Original list with duplicates:", numbers_list)

## Step 1: Convert the list to a set.
## This automatically removes all duplicate elements.
unique_numbers_set = set(numbers_list)
print("Set created from list (duplicates gone):", unique_numbers_set)

## Step 2: Convert the set back to a list.
## The new list will only contain the unique elements.
unique_numbers_list = list(unique_numbers_set)
print("Final list with duplicates removed:", unique_numbers_list)

## Note: This process does not preserve the original order of the elements
## because sets are an unordered data structure.

Save the file and run it from the terminal.

python ~/project/remove_duplicates.py

The output demonstrates the entire process, showing the original list, the intermediate set, and the final, de-duplicated list.

Original list with duplicates: [1, 5, 2, 3, 5, 1, 4, 2, 2, 5]
Set created from list (duplicates gone): {1, 2, 3, 4, 5}
Final list with duplicates removed: [1, 2, 3, 4, 5]

You have successfully applied your knowledge of sets to solve a common programming problem: removing duplicates from a list.

Summary

In this lab, you have learned the essential skills for working with sets in Python. You started by creating sets using different syntaxes and learned how they inherently enforce uniqueness. You practiced modifying sets by adding elements with add() and update(), and removing them with remove(), discard(), pop(), and clear(), noting the key differences between these methods.

Furthermore, you explored the core mathematical set operations—union (|), intersection (&), and difference (-)—which are fundamental to data analysis and algorithm design. Finally, you put this knowledge to practical use by implementing an elegant technique to remove duplicate items from a list, a common task in data cleaning and preparation. You are now equipped to use sets effectively in your Python programs.