Menu Close

Python Set Tutorial

Python Set

In this Python set tutorial, You will learn all about the Python set. set in Python is an unordered collection of items.
set in python is an immutable or unchangeable collection data type. In this guide, we will learn everything about sets like create a set, adding and removing items in the set, as well as perform operations on the set.

In the previous tutorial, we have seen the Python list and its complete method.

What is Python Set?

Set in python is a collection of elements that is immutable, unordered, and unindexed which means you can not access set elements by using their index number.

Python set written within a curly bracket ( {} ). set in python, does not allow duplicate elements that means you have not to assign duplicate elements inside the python set.

If you assign duplicate elements inside a python set then the python set treats that single element.

Create a Python set

set in Python is created by placing all items inside a curly bracket ( {} ), separated by commas, or you use the python set() method to create a set.

Python set accepts any number of items with different data types like ( number, string, and float ).
You can not assign duplicate items inside the set in Python If you assigned duplicate items inside the set, Python set to treat them one.


#Python set with same data tupe.
mySet = {'Maya','mariya','Alexa','Alex','John','Ashoka'}

#Creating python set with different data types 
mySet1 = {'Python', 22, 12.23, 'Machine Learning'}

#Creating set with duplicate items
mySet2 = {'Python', 22, 12.23, 'Machine Learning', 'Python'}


{'Alex', 'John', 'Alexa', 'Maya', 'Ashoka', 'mariya'}
{12.23, 'Python', 'Machine Learning', 22}
{12.23, 'Python', 'Machine Learning', 22}

Access Python set element

You can not access set element by using index number but still if you want
to access element then you can use for loop to iterate set.


mySet = {'Maya','mariya','Alexa','Alex','John','Ashoka'}
for x in mySet:



The data type of Python set

If you want to get the data type if set in python, Then you can use the type() built-in function.


#Access data type of set 
mySet1 = {'Python', 22, 12.23, 'Machine Learning'}

The output will be: <class ‘set’>

Get the length of the set

Sometimes you need to get the length of the set. To get the length of the set use len() built-in function.


mySet = {'Maya','mariya','Alexa','Alex','John','Ashoka'}

Output will be: 6.

Check item exist in set or not

To check a item exist in set, use in membership operator.


mySet = {'Maya','Mariya','Alexa','Alex','John','Ashoka'}
print('Maya' in mySet)

The output will be: True

Change items in set

Note:– If you created a set once then you are not able to change the set but you can definitely add items to the set.

Add item in set

To add item into the set use add() method.

mySet = {'Maya','mariya','Alexa','Alex','John','Ashoka'}

Remove item from set

a. To remove item from the set use remove() method.

mySet = {'Maya','mariya','Alexa','Alex','John','Ashoka'}

b. You can also use the discard() method to remove item from the set. remove() method raise error if the item does not exist but discard() method will not.

mySet = {'Maya','mariya','Alexa','Alex','John','Ashoka'}

c. If you want to remove last item from set then you can use pop() method.

mySet = {'Maya','mariya','Alexa','Alex','John','Ashoka'}

d. To completely delete the set in python you can use del keyword.

mySet = {'Maya','mariya','Alexa','Alex','John','Ashoka'}
del mySet

Clear the set

Some we need to clear all items from the set in python then we will need to use clear() method.

mySet = {'Maya','mariya','Alexa','Alex','John','Ashoka'}

Join two sets

To join two sets in python you have two methods first is a union() and the second is an update().

Using union() method:

a = {1, 2, 3, 4 ,5}
b = {'a', 'b',' c'}
c = a.union(b)


{1, 2, 3, 4, 5, 'b', 'a', ' c'}

Using update() method:

a = {1,2,3,4,5}
b = {'a','b','c'}


{1, 2, 3, 4, 5, 'b', 'c', 'a'}

Set Constructor

Python provides a set() constructor to create a set. with the help, if set() constructor you can create a set, set() constructor accept
any iterable( tuple, list, string )


#Create a python set using set() constructor

set1 = set((1,2,4,5,6,7,8,9,10))

set1 = set([1,2,4,5,6,7,8,9,10])

set1 = set('Programming')


{1, 2, 4, 5, 6, 7, 8, 9, 10}
{1, 2, 4, 5, 6, 7, 8, 9, 10}
{'r', 'g', 'i', 'P', 'm', 'a', 'n', 'o'}

Python set methods


In this tutorial, you have learned all about the Python set and its important methods. In Python, the set is an unordered and immutable or unchangeable collection of items.

If you want to perform more operations on the set, Then you have to learn all about set methods.

To learn about the Python set’s method, you can click on any particular method in the above set method table and learn about them.

I hope this python set will help you. If you like this article, Please comments and share it with your friends who want to learn Python programming.

For more information:- Click Here

Previous Article
Next Article

Related Posts

Leave a Reply