Set Data Type and Methods in Python
In this class, we discuss the set data type and methods in python.
For Complete YouTube Video: Click Here
Table of Contents
Set Data Type
Before going to understand set data type, the reader should have an idea of tuple data type. Click here.
First, we will refresh the concepts of set operations.
For complete basics on sets. Please check our discrete mathematics course.
Set is a group of distinct elements. I.e. duplicates are not allowed in the set.
Example:
A={1,2,5,6,7,8} is a set of elements.
B= {5,6,9,10,11} is a set of elements.
Set Union
Union operation is given a symbol U.
AUB means the union of all the elements in set A and set B. Duplicates are not taken
AUB set contains elements {1,2,5,6,7,8,9,10,11}
Set Intersection
The intersection operation is given a symbol ∩.
Intersection operation considers the common elements.
A ∩ B will result {5,6}
Set Difference
set difference operation is given symbol -.
The difference operation result elements are in A and not in B.
A-B will result {1,2,7,8}. 5,6 elements present in set B. so remove those elements from set A.
Set data type in python is defined to do these set operations.
Set is a collection that is unordered, not indexed, and no duplicates.
Unordered means how elements are defined are not displayed in the same order.
The data types we discussed previously are list and tuple. They are ordered.
How are the set elements saved in the memory?
Python uses a hash table to save the elements in the set.
Please watch our data structures course for a detailed explanation of hash tables.
Define and Access Set
Set is defined using {}.
a={1,2,3,4}
print(a)
The set is unordered.
Example:
a={“hello”,”this”,”is”,”python”,”program”}
print(a) will display {“is”,”hello”,”this”,”python”,”program”}
set within set and list within the set are not allowed.
Accessing elements in a set.
We do not have an index to access. We use a loop to iterate on elements.
The program is shown below.
a={"hello","this","is","python","program"} for i in a: print(i) Output: is hello this python program
Membership operator on set
a={“hello”,”this”,”is”,”python”,”program”}
print(“hello” in a) will display True.
Methods in Set
add method
The add method is used to add elements to the set.
We can not change the elements in a set. but we can add elements in a set.
a={“hello”,”this”,”is”,”python”,”program”}
a.add(“java”)
print(a) will display {“is”,”hello”,”this”,”java”,”python”,”program”}
update method
The update method is used to add multiple elements to a set.
a={“hello”,”this”,”is”,”python”,”program”}
a.update([“java”,”pascal”])
remove method
The remove method is used to remove an element in the set.
Example:
a={“hello”,”this”,”is”,”python”,”program”}
a.remove(“python”)
print(a) will display {“is”,”hello”,”this”,”program”}
If the element is not found in the set, the remove method will give an error.
Example
a.remove(“p”)
print(a) will give a key error.
discard method
The discard method will delete the element from the set and do not give an error if the element not found.
Example:
a={“hello”,”this”,”is”,”python”,”program”}
a.discard(“pascal”)
print(a) will display {“is”,”hello”,”this”,”python”,”program”}.
clear method
the clear method is used to remove all the elements in a set.
Example:
a={“hello”,”this”,”is”,”python”,”program”}
a.clear()
print(a) will display set(). i.e. empty set.
Set Operations
There are many set operations we discuss only few in this class.
Union operation
Example:
a = {1, 2, 3, 4, 5}
b = {4, 5, 6, 7, 8}
print(a|b) will display {1, 2, 3, 4, 5,6,7,8}
print(a.union(b)) will display {1, 2, 3, 4, 5,6,7,8}
We can do union operation using pipe symbol. or we can use union method.
Both are shown in above example.
Intersection Operation
Example:
a = {1, 2, 3, 4, 5}
b = {4, 5, 6, 7, 8}
print(a&b) will display (4,5}
print(a.intersection(b) will display (4,5}
Intersection operation can be done using symbol & and intersection Method.
Both are shown in the example.
Set Difference Operation
Example:
a = {1, 2, 3, 4, 5}
b = {4, 5, 6, 7, 8}
print(a-b) will display {1,2,3}
print(a.difference(b)) will display {1,2,3}
Set difference operation can be done using symbol – and using difference method.
Both are shown in the example.
In our next classes we find the use of set data type in programming.