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.


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 non-repeated, unique values within them.

>>> an_array = [1,2,2,3,4] # repeated values
>>> a_set = set({1,2,3,4}) # non-repeated, unique values
>>> a_set
{1, 2, 3, 4}

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:

>>> my_set
{1, 2, 3, 4}
>>> my_set.add(1)
>>> my_set
{1, 2, 3, 4} # No Change to the set 
>>> my_set.add(5)
>>> my_set
{1, 2, 3, 4, 5} # 5 is added to the set

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

>>> my_list = [1,2,2,3,4,5,5,1,3,6,7]
>>> my_set = set(my_list)
>>> my_set
{1, 2, 3, 4, 5, 6, 7} # prints out only the unique values


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.