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.
Table of Contents
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.
Example:
#Python set with same data tupe.
mySet = {'Maya','mariya','Alexa','Alex','John','Ashoka'}
print(mySet)
#Creating python set with different data types
mySet1 = {'Python', 22, 12.23, 'Machine Learning'}
print(mySet1)
#Creating set with duplicate items
mySet2 = {'Python', 22, 12.23, 'Machine Learning', 'Python'}
print(mySet2)
Output
{'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.
Example:
mySet = {'Maya','mariya','Alexa','Alex','John','Ashoka'}
for x in mySet:
print(x)
Output
Maya
Alexa
mariya
Ashoka
Alex
John
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.
Example:
#Access data type of set
mySet1 = {'Python', 22, 12.23, 'Machine Learning'}
print(type(mySet1))
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.
Example:
mySet = {'Maya','mariya','Alexa','Alex','John','Ashoka'}
print(len(mySet))
Output will be: 6.
Check item exist in set or not
To check a item exist in set, use in membership operator.
Example:
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'}
mySet.add('Mayank')
print(mySet)
Remove item from set
a. To remove item from the set use remove() method.
mySet = {'Maya','mariya','Alexa','Alex','John','Ashoka'}
mySet.remove('Maya')
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'}
mySet.discard('mariya')
c. If you want to remove last item from set then you can use pop() method.
mySet = {'Maya','mariya','Alexa','Alex','John','Ashoka'}
mySet.pop()
d. To completely delete the set in python you can use del keyword.
mySet = {'Maya','mariya','Alexa','Alex','John','Ashoka'}
del mySet
print(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'}
mySet.clear()
print(mySet)
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)
print(c)
Output
{1, 2, 3, 4, 5, 'b', 'a', ' c'}
Using update() method:
a = {1,2,3,4,5}
b = {'a','b','c'}
a.update(b)
print(a)
Output
{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 )
Example:
#Create a python set using set() constructor
set1 = set((1,2,4,5,6,7,8,9,10))
print(set1)
set1 = set([1,2,4,5,6,7,8,9,10])
print(set1)
set1 = set('Programming')
print(set1)
Output
{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
Conclusion
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