Python
In this tutorial we will learn about set in Python.
We briefly talked about set in the Python - Data Type tutorial.
A set is an unordered unique collection of items.
We create a set in Python using curly { }
brackets and separate the items using ,
comma.
In the following example we are creating a set of integer values.
mySet = {1, 2, 3}
set()
constructorWe use the set()
constructor to create a set in Python.
# set
mySet = set((1, 3.14, 'yusufshakeel', 1+2j, True))
print("Type of mySet:", type(mySet))
print(mySet)
Output
Type of mySet: <class 'set'>
{1, 3.14, 'yusufshakeel', (1+2j)}
We use the len
method to count the total number of items present in a given set.
The following Python program will print the total number of items in a given set.
# set
mySet = {1, 2, 3}
print(len(mySet)) # 3
Items in a set don't have any index value. So, we cannot access them using index like we do in list and tuple.
To access set items we use loop.
In the following Python program we are printing out all the items present in the given set.
# set
mySet = {1, 3.14, 'yusufshakeel', True, 1+2j}
# output
for item in mySet:
print(item)
The above code will print the following output.
1
3.14
yusufshakeel
(1+2j)
To check if an item exists in a given set we take help of the in - membership operator.
The in
operator will return True
if the item exists in the set. False
it it is not present.
In the following Python program we are checking if the items exists in the given set.
# set
mySet = {1, 3.14, 'yusufshakeel', True, 1+2j}
# output
print(1 in mySet) # True
print('hello' in mySet) # False
We cannot change items of a set once they are created.
We use the add
method to add a new item to a given set.
In the following Python program we are adding new item to a set.
# set
mySet = {1, 2, 3}
# add
mySet.add('Hello')
# output
print(mySet)
The above code will print a similar output.
{1, 2, 3, 'Hello'}
We use the update
method to add multiple items to a given set.
In the following Python program we are adding multiple items to the set.
# set
mySet = {1, 2, 3}
# add multiple items
mySet.update(['yusufshakeel', 3.14, True, 1+2j])
# output
print(mySet)
We will get the following output.
{1, 2, 3.14, 3, (1+2j), 'yusufshakeel'}
We use the remove
method to remove items from a given set.
If the item we are trying to remove does not exists then remove
will raise an error.
In the following Python program we are removing items from the set.
# set
mySet = {1, 3.14, 'yusufshakeel', True, 1+2j}
# remove
mySet.remove(1+2j)
# output
print(mySet)
We will get the following output.
{1, 3.14, 'yusufshakeel'}
If the item we are trying to remove does not exists then remove
will raise an error.
In the following Python program we are trying to remove an item that does not exists in the set.
# set
mySet = {1, 3.14, 'yusufshakeel', True, 1+2j}
# remove
mySet.remove('unknown')
# output
print(mySet)
We will get an error message like the following.
Traceback (most recent call last):
File "/Users/yusufshakeel/PycharmProjects/HelloWorld/Set.py", line 5, in
mySet.remove('unknown')
KeyError: 'unknown'
To avoid having errors like the one we encountered with the remove method we use the discard
method.
So, if the item does not exists in the set then the discard
method will not raise any error.
In the following example we are trying to remove an item that does not exists in the set using the discard
method.
# set
mySet = {1, 3.14, 'yusufshakeel', True, 1+2j}
# discard
mySet.discard('unknown')
# output
print(mySet)
On running the above code no error is raised and we get the following output.
{'yusufshakeel', 1, 3.14, (1+2j)}
We use the clear
method to clear the items of a given set.
In the following Python program we are clearing out the items of a given set.
# set
mySet = {1, 3.14, 'yusufshakeel', True, 1+2j}
print("before", mySet)
# clear
mySet.clear()
print("after", mySet)
We will get the following output.
before {1, 3.14, 'yusufshakeel', (1+2j)}
after set()
We use the del
keyword to delete a set along with the items.
In the following Python program we are deleting a given set.
# set
mySet = {1, 3.14, 'yusufshakeel', True, 1+2j}
del mySet
ADVERTISEMENT