Python Sets


Python Sets are one of the four data types that are used for data collection storage. The other three data types that are used fort his purpose are lists, tuples and dictionaries. In this Python Programming Course lesson, our focus is Python Sets. We will learn the details of thiss data type of python with various coding examples.


With a set, we can store multiple items in single variable. These items in the python sets are unordered and unchangebale items. We can not change any item in s set. And we can not use duplicate items in a set. All the items must be used one time. If we use an item multiple times, then


We can use any data types in sets. This data type can be a string, a number or in any other data type. These data types can be in different set sor a one set can include different data types.


Other Python Set Lessons:


Python Set Creation


To create a python set, we use curly brackets. Inside these curly brackets, we put the items of the set. Below, you can find a set example created with string items:


dwarves = {"Thorin", "Balin", "Dwalin"}


We can also use set method to create a set like below:


dwarves = set(("Thorin", "Balin", "Dwalin"))


Let’s use type method to see the type of “myset” above.


dwarves = set(("Thorin", "Balin", "Dwalin"))


The output of this code will be:


<class 'set'>


You can practice below!

Now, let’s create different set examples with differetn data types:




{32, 2, 5, 45, 14, 18, 23}



items={True, 5, 2.7, "Gimli", False, 34}
{False, True, 2.7, 'Gimli', 34, 5}





Python Set Operations


With python sets, we can use different operations. Let’s give some examples to these operations.


We can combine two sets into one with the operator “|”. The same can be done with python union method.


dwarves1 = {"Thorin", "Balin", "Dwalin"}
dwarves2 = {"Gimli", "Bofur", "Thorin"}
print(dwarves1 | dwarves2)


As the output, we will see the combination of these sets. But the double items will be only once in the new set.


{'Balin', 'Dwalin', 'Bofur', 'Thorin', 'Gimli'}




As a number set example, the below code will be similar:


numbers1 = {2,5,7}
numbers2 = {9,2,7,22,10}
print(numbers1 | numbers2)


{2, 5, 7, 9, 10, 22}




We can find the common items in a python set with “&” operator. This can be done also with intersection method.


numbers1 = {2,5,7}
numbers2 = {9,2,7,22,10}
print(numbers1 & numbers2)


{2, 7}


There are also other operations used with python sets. We will discuss them in the Python Set Operations lessons with different examples.



Python Set Methods


There are different python set methods. Let’s give some examples for this methods.


First of all let’s remember len method as we have discusses in the previous list and tuple lessons. The aim of this method is to measure the length of the set.




Here, the output of this python set code will be:






Now, let’s use this method in a string set:


dwarves = {"Thorin", "Balin", "Dwalin"}


The output of this code will be:






The other important method used with python sets is add method. With add method, we can add a new item to the set.


dwarves = {"Thorin", "Balin", "Dwalin"}


As you can see below, the new item has added to the set:


{'Balin', 'Gimli', 'Thorin', 'Dwalin'}




We can use another method, remoe method to remove any item in the set.


dwarves = {"Thorin", "Balin", "Dwalin"}


{'Balin', 'Dwalin'}


Or we can clear the items in the set with set clear method.


dwarves = {"Thorin", "Balin", "Dwalin"}




We can also compare two sets and print find the differences of thesse two sets with set methods. Here, to do this, we use difference method of python. In the below example, we will find the item that exist in the fist set but not in the second set.


dwarves1 = {"Thorin", "Balin", "Dwalin"}
dwarves2 = {"Gimli", "Balin", "Thorin"}
dwarves3 = dwarves1.difference(dwarves2)


The only item that exist in the first set but not in the second set is “Dwalin”. So the output of this python code will be:






Beside these methods, there are also other methods used with python sets. We will discuss them in python set methods lesson with different examples detailly.


In this lesson, we have learned how to use Python Sets. We have given different set examples for various usage of this data type. Generally, Python sets are very useful to find duplicate items in a set. Beside, common math operations can be done with sets like unions and intersections.


Back to: Python Programming Course > Python Sets

Leave a Reply

Your email address will not be published. Required fields are marked *

Python Programming Course