A Python set is a built-in data type in python that is used to store a set of multiple items in a single variable. Sets have a few unique properties: they are unordered and unindexed. Furthermore, sets cannot contain duplicate values but can accommodate any kind of object as long as they are immutable. For instance, a set can only contain strings, numbers or tuples but not dictionaries or other lists.



Creating a Python Set

There are two main ways of creating a set in Python. We need to choose a name for the set, then places a set of items within the curly braces separated by a comma. A set can also be created using a built-in set() function. Now in the example below we are going to create a set by the name “animals” in addition we’re just going to use strings i.e {“elephant”, ”tiger”, ”kangaroo”, ”armadillo”}.





Note that the elements in the output are not in the same order as entered in the code, that’s because sets are unordered. Another way that you could create a set is to simply call it with curly brackets like this: animals = {}. However, if we were to leave it blank, python would assume that this is a dictionary. But if we proceed and fill out values within the curly braces, then we would have created a new set.



A list can contain any type of object as long as it is immutable. We can also add numbers, tuples to the above set as shown in the code below.



Adding and removing items from a set

We can also add new items to a set using the “animals.add()” keyword; we can also remove them using the “animals.remove()” method and thereafter print out the resulting set. For example, we can remove ‘kangaroo’ from the set and add ‘lion’ as shown below.


The first print statement outputs the original set of animals, while the second one outputs the set after the lion has been added and finally the last print statement outputs the set after ‘kangaroo’ has been removed.

One really useful thing you can do with the remove method is that you can easily use it to remove duplicates from a set.


Learn python sets online


The Union and intersection of sets

In order to compare sets with an intention of drawing insights, we can use the union and the intersection among other options. This will be easy if you are already familiar with the concept of Venn diagrams in mathematics. In order to be able to do a comparison we are going to create a new set that goes by the name zoo = {‘monkey’,  ‘giraffe’, ’elephant’, ’lion’}. The union and intersection of the two sets are performed as follows.



The union of the two sets return a set containing elements from both the two sets, while the intersection returns elements that exist in both sets.



In our Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.

Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.


Learn to code and change your career!





Not sure if programming is for you? With CodeBerry you’ll like it.