17.set in Python
17.set in Python
17.set in Python
Home /
Tutorial /
Python Sets
Python Sets
By Manoj 6.6 K Views 20 min read Updated on March 9, 2022
In this module of the Python tutorial, we will learn in detail about the set data type in Python. We will further learn the
advantages of the set data type over the list data type. This module also highlights topics such as how to instantiate a Set and
various operations in sets, and toward the end of this module we will also learn about various methods in Python set data type.
Python Tutorial
Variables
Numbers in Python
String in Python
Python Lists
Tuple in Python
Python Sets
Python Dictionary
Python Operators
Type conversion in
Python
Python If Else
Statements
Python Functions -
Define & Call a Functions
in Python
Lambda Function in
Python
Python Built in
Functions with Examples
Python Arrays
Python Dates
Python JSON
Python RegEx
PIP Python
Exception Handling in
Python
Enumerate Function in
Python
Python Queue
Python Set
A set in Python is mutable, iterable, and does not have any duplicate elements. It is an unordered collection of elements
which means that a set is a collection that stores elements of different Python Data Types. Remember that a set in Python
doesn’t index the elements in a particular order. Let us look at some of the properties of sets in Python. Sets in Python are
usually used to perform some mathematical functions such as union, intersection, etc.
Become a master of Python by going through this online Python Course in Toronto!
One of the major advantages of using sets in Python is that unlike some other data types like Python Lists, sets contain a
highly optimized method for the sole purpose of checking whether a particular element is included in a set or not.
Also, since sets in Python are mutable, we can add and remove elements from a set; however, every element that is added
in the set must be unique and immutable, that is, we cannot change elements once they have been added.
In this module, we will learn all about sets in order to get started with them. Following is the list of all topics that we will be
covering.
print(myset)
Output:
2. Using the in-built set() method with the elements that we want to add as the parameters
print(myset)
Output:
Remember that once a set is created we can’t change that set. We can only add elements. We can remove and then add
elements but cannot change the existing elements. Now, let us see how to add items in a Python set.
myset.add(“orange”)
print(myset)
Output:
2. Using update()
print(myset)
Output:
myset.remove(“banana”)
print(myset)
output:
{‘cherry’, ‘apple’}
Note: If the item to be removed does not exist, remove() will raise an error.
2. Using discard():
myset.discard(“banana”)
print(myset)
Output:
{‘cherry’, ‘apple’}
Note: If the item to be removed does not exist, discard() will not raise an error.
3. Using pop():
Remember that pop() will remove the last item of a set. Since sets are unordered, we should avoid performing pop() in sets.
x = myset.pop()
print(x)
print(myset)
Output:
{‘banana’, ‘apple’}
print(len(myset))
Output:
1. Using clear():
myset.clear()
print(myset)
2. Using del():
del myset
print(myset)
Output:
As discussed above, sets in Python are used to carry out mathematical set operations such as union, intersection, etc. Let’s
see a few examples of these mathematical operations.
Learn end-to-end Python concepts through the Python Course in Hyderabad to take your career to a whole new level!
Sets Union
To perform union, we use “|” operator. We can also use an in-built method called union(), which will give the same result.
The result of the union of two sets, say Set A and Set B, is a set containing the elements of both sets.
The following code block shows the union of Set A and Set B:
Set_A = {1,2,3,4,5}
Set_B = {4,5,6,7}
print(Set_A | Set_B)
Output:
{1, 2, 3, 4, 5, 6}
Set Intersection
To perform an intersection, we use ‘&’ operator. We can also use the in-built Python Function named intersection() to get
the same result.
The intersection of two sets say Set A and Set B, can result in a set that contains the elements that are common in both
sets, that is, the overlapping part in the diagram below.
The following code block shows the union of Set A and Set B:
Set_A = {1,2,3,4,5}
Set_B = {4,5,6,7}
Output:
{4, 5}
Are you interested in learning Python from experts? Enroll in our Python Course in Bangalore now!
Method Description
update() It updates a set with the union of this set and others.
difference() It returns a set containing the difference between two or more sets.
difference_update() It removes the items in a set that are also included in another, specified set.
intersection_update() It removes items in a set that are not present in another, specified set(s).
Frozenset in Python
frozenset() is an inbuilt Python function that takes an iterable object as input and makes it immutable. It Simply freezes the
iterable objects and makes them unchangeable. The frozenset() function returns an unchangeable frozenset object (which
is like a set object, only unchangeable).
In Python, frozenset() is the same as set except the forzensets are immutable which means that elements from the
frozenset cannot be added or removed once created. This function takes input as any iterable object and converts them
into an immutable object. The order of elements is not guaranteed to be preserved.
x = frozenset(mylist)
print(x)
OrderedSet([1, 2, 3])
Set List
Sets are unordered collections of items Lists are ordered collections of items