Sets are one of the built-in data structures in Python. When you need to work with a non-repeating collection of elements, you’ll use the set as the go-to data structure. Over the following several sections, we’ll go over the basics of python sets and the set methods you can use to work with them. We will then learn how to perform common set operations in Python. Let’s begin!
Basics of Python Sets
In Python, a set is an unordered collection of non-repeating elements. This means the elements in a set should all be distinct. You can add and remove elements from a set; therefore, the set is a mutable collection. It can contain elements of different data types. However, the individual elements in a set should be hashable. In Python, an object is said to be hashable if its hash value never changes. Most immutable objects such as Python strings, tuples, and dictionaries are hashable. We’ll learn about creating sets in detail. For now, consider the following two sets: The first set contains three numbers, a tuple, and a string. The set initialization runs without error. Whereas the second set contains a list instead of a tuple. A list is a mutable collection, it cannot be hashed, and the initialization throws a TypeError.
How to Create a Python Set
We’ll start by learning how to create a set in Python.
#1. Using Explicit Initialization
You can create a set in Python by specifying the elements of the set, separated by commas (,) and enclosed in a pair of curly braces {}. If you’ve worked with Python lists before, you know that [] initializes an empty list. Even though a Python set is enclosed in a pair of curly braces {}, you cannot use a pair {} to initialize a set. This is because {} it initializes a Python dictionary and not a Python set. You can again call the type() function to verify that py_set it is a dictionary (dict).
#2. Using the set() Function
If you’d like to initialize an empty set and then add elements to it, you can do so using the set() function.
#3. Casting Other Iterables into a Set
Another way to create sets is to cast other iterables, such as lists and tuples, into sets, using set(iterable). In the above example, py_list contains ‘C’ twice. But in py_set4, ‘C’ appears only once, as the set is a collection of distinct elements. This technique of casting into the set is often used to remove duplicates from Python lists.
How to Add Elements to a Python Set
Let’s start by creating an empty set py_set and work with it for the remainder of this tutorial.
#1. Using the .add() Method
For clarity, we’ll add elements to the Python set and print out the set at each step. ▶️ Let’s add the string ‘Python’ as an element to py_set. Next, we’ll add another element. It’s important to understand that the .add() method only adds an element to the set if it is not already present. If the set already contains the element you’d like to add, the add operation has no effect. To verify this, let’s try adding ‘C++’ to py_set. The set contains ‘C++’, so the add operation has no effect. ▶️ Let’s add a few more elements to the set.
#2. Using the .update() Method
So far, we’ve seen how to add elements to the existing set – one element at a time. What if you’d like to add more than one element to a sequence of elements? This method is helpful when you want to add a collection of elements to a set without creating another object in memory. In the next section, let’s learn how to remove elements from a set.
Remove Elements from a Python Set
Let’s consider the following set (py_set before the update operation).
#1. Using the .pop() Method
set.pop() removes an element randomly from the set and returns it. Let’s call the pop method on py_set and see what it returns. This time, the call to .pop() method returned the string ‘Rust’. Note: Because the .pop() method returns an element at random, when you run the code at your end, you might as well get another element. When we examine the set, ‘Rust’ is no longer present in the set.
#2. Using the .remove() and discard() Methods
In practice, you may want to remove specific elements from the set. To do this, you can use the .remove() and .discard() methods. set.remove(element) removes elements from the set. If we try to remove an element not present in the set, we’ll run into a KeyError. Let’s take a look at py_set it again. We now have three elements. With the syntax set.discard(element), the .discard() method also removes elements from the set. However, it differs from the .remove() method in that it does not raise a KeyError when we try to remove an element that is not present. If we try to remove ‘Scala’ (which does not exist) from the list using the .discard() method, we see no error.
Access Elements of a Python Set
So far, we’ve learned how to add and remove elements from Python sets. However, we haven’t yet seen how to access individual elements in a set. As a set is an unordered collection, it is not indexable. Therefore, if you try to access the elements of a set using the index, you’ll run into an error, as shown. So how do you access elements in a set? There are two common ways to do this:
Loop through the set and access each element Check if a particular element is a member of the set
▶️ Loop through the set and access elements using a for loop. In practice, you may want to check if a given element is present in the set using the in operator. In this example, py_set contains ‘C++’ and does not contain’ Julia’ and the in operator returns True and False, respectively.
Find the Length of a Python Set
As seen earlier, you can use the len() function to get the number of elements present in a set.
How to Clear a Python Set
To clear a set by removing all elements, you can use the .clear() method. Let’s call the .clear() method on py_set. If you try to print it out, you will get set() – indicating that the set is empty. You can also call the len() function to verify that the length of the set is zero. So far, we’ve learned how to perform basic CRUD operations on Python sets:
Create: Using set() function, type casting, and initialization Read: Access elements of the set using loops and in operator for membership testing Update: Add, remove elements from sets, and update sets Delete: Clear a set by removing all elements from it
Common Set Operations, Explained with Python Code
Python sets also allow us to perform the basic set operations. We’ll learn about them in this section.
#1. Union of Sets
In set theory, the union of two sets is the set of all elements in at least one of the two sets. If there are two sets, A and B, then the union contains elements that are present only in A, only in B, and the elements present in both A and B. To find the union of sets, you can use the | operator or the .union() the method with the syntax: setA.union(setB). Set union is a commutative operation; so A U B is the same as B U A. Let’s verify this by interchanging the positions of setA and setB in the .union() method call.
#2. Intersection of Sets
Another joint set operation is this intersection of two sets, A and B. Set intersection operation returns a set that contains all the elements present in both A and B. To compute the intersection, you can use the & operator or the .intersection() method, as explained in the code snippet below. In this example, element 9 is present in both setA and setB; so the intersection set contains only this element. Like the set union, the set intersection is also a commutative operation.
#3. Set Difference
Given any two sets, union and intersection help us find the elements present in both and at least one of the sets, respectively. On the other hand, set difference helps us find the elements present in one set but not in the other. Clearly, A\B is not the same as B\A, so the set difference is not a commutative operation. – setB.difference(setA) gives the set of elements that are present only in setB and not in setA.
#4. Symmetric Set Difference
While set intersection gives us elements present in both sets, the symmetric set difference returns the set of elements present in exactly one of the sets. Consider the following example. To compute the symmetric difference set, you can use the ^ operator or the .symmetric_difference() method. The elements 10 and 12 are present in both setA and setB. So they are not present in the symmetric difference set. As the symmetric set difference operation collects all elements that appear in exactly one of the two sets, the resultant set is the same regardless of the order in which the elements are collected. Therefore, a symmetric set difference is a commutative operation.
#5. Subsets and Supersets
In set theory, subsets and supersets help understand the relationship between two sets. Given two sets A and B, set B is a subset of set A if all elements in set B are also present in set A. And set A is the superset of set B. Consider the example of two sets: languages and languages_extended. In Python, you can use the .issubset() method to check if a given set is a subset of another set. In this example, languages is a subset of languages_extended. Similarly, you can use the .issuperset() method to check if a given set is a superset of another set. As languages_extended is a superset of languages, languages_extended.issuperset(languages) returns True, as seen above.
Conclusion
I hope this tutorial helped you understand the working of Python sets, the set methods for CRUD operations, and common set operations. As a next step, you can try using them in your Python projects. You can check out other in-depth Python guides. Happy learning!