Python contains several data structures, like lists, tuples, dictionaries, and sets. Each structure stores data in different ways. Lists, tuples, and dictionaries store data in a format where order matters and they allow storing duplicate items. Sets, on the other hand, store only unique items preventing data redundancy. Therefore, if you want to store data where the order and frequency of items don’t matter, you should use sets.
In this tutorial, we’ll show you how to perform some basic operations with the set data structure in Python. You don’t need to import any libraries to use sets in Python. They’re available by default when you install Python, so let’s jump right in.
Creating a Set in Python
To create a set in Python, you can simply write Set()
which returns an empty set. To find all the attributes and methods of a set, you can pass the set object to the dir()
method, like this:
my_set = set()
print(dir(my_set))
Output:
['__and__',
'__class__',
'__contains__',
'__delattr__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__gt__',
'__hash__',
'__iand__',
'__init__',
'__init_subclass__',
'__ior__',
'__isub__',
'__iter__',
'__ixor__',
'__le__',
'__len__',
'__lt__',
'__ne__',
'__new__',
'__or__',
'__rand__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__ror__',
'__rsub__',
'__rxor__',
'__setattr__',
'__sizeof__',
'__str__',
'__sub__',
'__subclasshook__',
'__xor__',
'add',
'clear',
'copy',
'difference',
'difference_update',
'discard',
'intersection',
'intersection_update',
'isdisjoint',
'issubset',
'issuperset',
'pop',
'remove',
'symmetric_difference',
'symmetric_difference_update',
'union',
'update']
The above output shows that a Python set contains methods like add()
, clear()
, remove()
and discard()
. You don’t have to open your browser to figure out what these methods do. You can pass the method name to the Python help
command. The help
command returns a brief explanation of any method or attribute you pass to it. For instance, the following script explains what the clear()
method of a Python set does.
my_set = set()
help(my_set.clear)
Output:
Help on built-in function clear:
clear(...) method of builtins.set instance
Remove all elements from this set.
From the output, you can see that the clear()
method removes all the elements from a set.
Adding Items to a Set
To add new elements to a set, you have two options. The first option is to create an empty set and then call the add()
method. The following script creates an empty set and adds 4 elements to the set. Notice, you can add elements of different types in a set. In this example, we included numbers, a boolean and a string. You’ll also notice the order of the output doesn’t match the order we added elements to our set. That’s because the ordering of elements in a set doesn’t matter.
my_set = set()
my_set.add(10)
my_set.add(5)
my_set.add("Hello")
my_set.add(True)
print(my_set)
Output:
{True, 10, 'Hello', 5}
The second option for creating a set is to pass a list of items to the pair of parenthesis as you initially create the set, as shown below.
my_set = set([20, "Hello2", False, 30])
print(my_set)
Output:
{'Hello2', False, 20, 30}
As discussed earlier, a set can only contain unique elements. If you add an item that already exists in a set, the new item will not be added. For instance, if you add the integer 20 to the set
my_set.add(20)
print(my_set)
Output:
{'Hello2', False, 20, 30}
You can find the total number of elements in a set by passing the set into the len()
method as shown in the following example:
print(len(my_set))
Output:
4
Removing Items from a Set
Just like when adding items to a Python set, there are a couple ways to remove an item from a set. One way to remove items from a set is to pass the item to the remove()
method. If the item exists, it will be removed.
The following script removes the item 20 from the set my_set.
my_set = set([20, "Hello2", False, 30])
my_set.remove(20)
print(my_set)
Output:
{'Hello2', False, 30}
If an item doesn’t exist in a set, however, the remove()
method throws an exception:
my_set.remove(15)
Output:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-44-70ed28938912> in <module>
----> 1 my_set.remove(15)
KeyError: 15
You can use the discard()
method to remove an item from a set if you don’t want to raise an exception just in case the item you want to remove doesn’t exist. For instance, the following script will try to remove the item 15 from the set if it exists in the set. In our example, it doesn’t exist, but discard
is able to handle the attempt without raising an exception.
my_set.discard(15)
Clearing a Set
To clear a set, you can call the clear()
method. The following script shows the elements in a set before and after a set is cleared.
my_set = set([20, "Hello2", False, 30])
print(my_set)
my_set.clear()
print(my_set)
Output:
{'Hello2', False, 20, 30}
set()
Code More, Distract Less: Support Our Ad-Free Site
You might have noticed we removed ads from our site - we hope this enhances your learning experience. To help sustain this, please take a look at our Python Developer Kit and our comprehensive cheat sheets. Each purchase directly supports this site, ensuring we can continue to offer you quality, distraction-free tutorials.
Finding the Union and Intersection of Two Sets
Just like sets in arithmetic, you can find the union and intersection of two sets in Python as well.
The intersection of two sets returns only the common elements between two sets. To find the intersection of two sets, use the intersection()
method. To demonstrate this, the following script finds the intersection of sets
my_set1 = set([20, "Hello2", False, 30, 40])
my_set2 = set([10, "Evening", False, 30, 15])
inter_set = my_set1.intersection(my_set2)
print(inter_set)
The boolean “False” and the integer 30 are the two elements in common between our two sets, hence they are returned by the intersection method.
Output:
{False, 30}
Python also allows a shorthand notation for calculating the intersection between two sets via the & operator.
inter_set = my_set1 & my_set2
The union of two sets returns a set that contains the combination of all the unique elements in the two input sets. The union()
method returns the union of two sets in Python.
my_set1 = set([20, "Hello2", False, 30, 40])
my_set2 = set([10, "Evening", False, 30, 15])
union_set = my_set1.union(my_set2)
print(union_set)
Output:
{False, 40, 10, 15, 'Hello2', 'Evening', 20, 30}
Notice that the items that appeared in both sets only appear once in our
Like intersections, Python also has a shorthand for calculating unions using the the | operator.
union_set = my_set1 | my_set2
Finding the Difference Between Sets
The difference of two sets returns all the elements from the first set that are not present in the second set.
You can find the difference between two sets using the difference()
function. The following script finds the difference between
my_set1 = set([20, "Hello2", False, 30, 40])
my_set2 = set([10, "Evening", False, 30, 15])
diff_set = my_set1.difference(my_set2)
print(diff_set)
Output:
{40, 20, 'Hello2'}
It’s important to mention that, just like when subtracting two numbers, the order in which you calculate the difference is important. For example my_set1.difference(my_set2)
returns elements from my_set2.difference(my_set1)
returns elements from
diff_set = my_set2.difference(my_set1)
print(diff_set)
Output:
{'Evening', 10, 15}
This is easier to visualize when using the shorthand notation for Python set differences, -.
diff_set = my_set1 - my_set2
Output:
{'Evening', 10, 15}
Checking if a Set is a Subset of Another Set
A set is considered a subset of another set if all the items in the first set are also present in the second set. You can check if a set is a subset of another set using the issubset()
method. The following script returns True
because all elements in
my_set1 = set([20, "Hello2", False, 30, 40])
my_set2 = set([20, False, 30])
subset_set = my_set2.issubset(my_set1)
print(subset_set)
Output:
True
The corollary to this is that since False
.
my_set1 = set([20, "Hello2", False, 30, 40])
my_set2 = set([20, False, 30])
subset_set = my_set1.issubset(my_set2)
print(subset_set)
Output:
False
Checking if a Set is a Superset of Another Set
A set is a superset of another set if it contains all the items that are present in the other set. For instance, in the following script issuperset()
method, which returns a Boolean value.
my_set1 = set([20, "Hello2", False, 30, 40])
my_set2 = set([20, False, 30])
super_set = my_set1.issuperset(my_set2)
print(super_set)
Output:
True
In the example above, every element in
Just like with difference
and issubset
, the order of your calculation is important. Every element in
We’ve now talked about both supersets and subsets, so in what scenario would two sets be both subset and superset of each other? Answer: If our two sets have exactly the same elements.
Checking if Two Sets are Disjoint
Two sets are disjoint sets if they have zero common elements. In other words, each set is entirely unique. To check if two sets are disjoint, you can call the disjoint()
method as shown in the following example. The following script returns False
, since
my_set1 = set([20, "Hello2", False, 30, 40])
my_set2 = set([20, False, 30])
disjoint_set = my_set1.isdisjoint(my_set2)
print(disjoint_set)
Output:
False
The following script returns True
because
my_set1 = set([20, "Hello2", False, 30, 40])
my_set2 = set([21, True, 25])
disjoint_set = my_set1.isdisjoint(my_set2)
print(disjoint_set)
Output:
True
By now, you should have a solid understanding of the basics of Python set operations. We’ve shown you how to add elements to a set, remove elements from a set, calculate how many elements are in a set and completely clear a set. We’ve also shown you how to return the items in two sets that are alike and the items in two sets that are different. We’ve even introduced more sophisticated Python set topics, like subsets, supersets and disjoint sets. If you want to learn more about Python sets or any other Python topic, please join us using the form below.
Code More, Distract Less: Support Our Ad-Free Site
You might have noticed we removed ads from our site - we hope this enhances your learning experience. To help sustain this, please take a look at our Python Developer Kit and our comprehensive cheat sheets. Each purchase directly supports this site, ensuring we can continue to offer you quality, distraction-free tutorials.