In this Python set tutorial, You will learn all about the Python set. Set in Python is an unordered collection of items which means the order of items doesn’t fix. Set in Python is a mutable or changeable collection data type. In this guide, we will learn everything about sets like creating a set, adding and removing items in the set, as well as performing operations on the set.
In the previous tutorial, we have seen the Python list and its complete methods.
Headings of Contents
- 1 What is Python Set?
- 1.1 Create a Python Set
- 1.2 Access Python Set Element
- 1.3 The Data Type of the Python Set
- 1.4 Get the Length of the Set
- 1.5 Check item Exists in the Set or not
- 1.6 Change items in the Set
- 1.7 Add item in the Set
- 1.8 Remove the item from the Set
- 1.9 Clear the Set
- 1.10 Join two sets
- 1.11 Set Constructor
- 1.12 Python Set Methods
- 1.13 Important Points about the Set:-
- 1.14 Conclusion
What is Python Set?
Set in Python is a collection of elements that is mutable or changeable, 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 which means you do 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 assign duplicate items inside the set, Python set to treat them as 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 a set element by using an index number but still, if you want to access the element then you can use for loop to iterate the 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 the Python Set
If you want to get the data type 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 the len() built-in function.
Example:
mySet = {'Maya','mariya','Alexa','Alex','John','Ashoka'}
print(len(mySet))
The output will be 6.
Check item Exists in the Set or not
To check an item that exists in the 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 the 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 the Set
To add an item to the set use add() method.
mySet = {'Maya','mariya','Alexa','Alex','John','Ashoka'}
mySet.add('Mayank')
print(mySet)
Remove the item from the Set
a. To remove an item from the set using the remove() method.
mySet = {'Maya','mariya','Alexa','Alex','John','Ashoka'}
mySet.remove('Maya')
b. You can also use the discard() method to remove items from the set. remove() method raises an error if the item does not exist but the discard() method will not.
mySet = {'Maya','mariya','Alexa','Alex','John','Ashoka'}
mySet.discard('mariya')
c. If you want to remove the last item from the set then you can use the pop() method.
mySet = {'Maya','mariya','Alexa','Alex','John','Ashoka'}
mySet.pop()
d. To completely delete the set in Python you can use the 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 the 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
Important Points about the Set:-
- Set in Python is created using curly bracket ( { } ).
- Another way to create a Python set is the set() function.
- Python Set is mutable or changeable.
- Python Set does not allow duplicate items.
- The Set is faster than the list.
Conclusion
In this tutorial, you have learned all about the Python set and its important methods. In Python, the set is an unordered and mutable or changeable 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 comment and share it with your friends who want to learn Python programming.
For more information:- Click Here