Under Construction
In this tutorial we are going to look at the set
data structure in computer science and how you can leverage it within your applications. We’ll be covering this concept using the Python programming language.
Sets in Mathematics
Understanding Sets and basic Set Theory in Mathematics is a fundamental skill. The idea of a Set has been translated directly from mathematics into programming languages such as Python, and with this Set data structure, comes some incredibly useful functions such as union()
, issubset()
, intersection()
, isdisjoint()
that have also been translated directly over from Mathematics.
Sets aren’t just a fundamental skill in Mathematics, throughout your programming career you will likely encounter a wide number of different problems that can be far more easily solved with the use of Sets.
Introduction
A set
is a data structure that can store any number of unique
values in any order you so wish. Set’s are different from arrays in the sense that they only allow nonrepeated, unique values within them.
Thankfully, programming languages tend to have the ability to convert the likes of an array
into a set
with minimal fuss.
Let’s take a look at an example of this in Python. We’ll start off by defining an_array
which will contain a number of repeated elements.
We can then create a set using the set()
function and pass in our an_array
. This will automatically create a set of the unique values in our array for us. We can then view these unique values by printing out the contents of our set.


This can also be used for strings:


Adding Values to Sets
When you try to add values to a set
in Python, it will only insert the value if that value does not already exist within the set:


This means we can very easily use the set
datastructure to parse all of the unique values from a list of any size:


Conclusion
Hopefully you found this article on the set
data structure useful! If you require further help then please feel free to let me know in the comments section below or by tweeting me: @Elliot_f.