This tutorial will discuss how to create a Python empty set and describe various operations that can be performed on the set.
In Python, a set is a data structure that stores unique elements, ensuring that no duplicates are present. It will not allow duplicates. A set is represented by {}.
Example: my_set={1,2,3,4,5}
How to Create an Empty Set in Python
Let’s first see how to create an empty set in Python. We can create an empty set by using the set() function.
Syntax:
set()
To display the data structure, use the type() function.
Syntax:
type(my_set)
Example: We will create an empty set and display its type in this example.
#create a set my_set=set() #display the type print(type(my_set))
Output:
<class 'set'>
Access items in a set
To access the elements in a set, we can use a loop to iterate over all the elements.
Example: In this example, we will create two sets. One set with 5 integer elements and the other set with 5 string elements, and display using a for loop.
#create a set with five integers
my_set1={23,45,43,22,34}
#create a set with five strings
my_set2={"php","python","java","html/css","IOT"}
#iterate through set1
for i in my_set1:
print(i,end=" ")
print()
#iterate through set2
for i in my_set2:
print(i,end=" ")
Output:
34 43 45 22 23 python java IOT html/css php
We can display the entire set using the print() statement.
#create a set with five integers
my_set1={23,45,43,22,34}
#create a set with five strings
my_set2={"php","python","java","html/css","IOT"}
#display
print(my_set1,my_set2)
Output:
{34, 43, 45, 22, 23} {'python', 'java', 'IOT', 'html/css', 'php'}
Add items to a Set
We can add elements to the set by using the add() function.
Syntax:
my_set.add("element")
Where my_set is the input set.
Example 1: We will add 90 to the first set and “R” to the second set.
#create a set with five integers
my_set1={23,45,43,22,34}
#create a set with five strings
my_set2={"php","python","java","html/css","IOT"}
#add 90 to set1
my_set1.add(90)
#add "R" to set2
my_set2.add("R")
#display
print(my_set1,my_set2)
If we want to add the existing elements to the set, it won’t allow but will not return an error.
Example 2: In this example, we will add 22 to the first set and “php” to the second set.
#create a set with five integers
my_set1={23,45,43,22,34}
#create a set with five strings
my_set2={"php","python","java","html/css","IOT"}
#add 22 to set1
my_set1.add(22)
#add "php" to set2
my_set2.add("php")
#display
print(my_set1,my_set2)
Output:
{34, 43, 45, 22, 23} {'python', 'java', 'IOT', 'html/css', 'php'}
Update set
We can also update all existing elements in the set using the update() function.
Syntax:
my_set.update(["elements"])
where,
- my_set is the input set
- elements are the elements that update the existing elements, which are given through a list.
Example: In this example, we will update the integer set with the string set and the string set with the integer set.
#create a set with five integers
my_set1={23,45,43,22,34}
#create a set with five strings
my_set2={"php","python","java","html/css","IOT"}
#before updation
print(my_set1,my_set2)
#update set2 with set1
my_set1.update(["php","python","java","html/css","IOT"])
#update set1 with set2
my_set1.update([23,45,43,22,34])
#after updation
print(my_set1,my_set2)
Output:
{34, 43, 45, 22, 23} {'python', 'java', 'IOT', 'html/css', 'php'}
{34, 'php', 'python', 'java', 43, 45, 'IOT', 'html/css', 22, 23} {'python', 'java', 'IOT', 'html/css', 'php'}
Length of the set
We can find the set’s length (total number of elements) by using the len() function.
Syntax:
len(my_set)
Where my_set is the input set.
Example: This example will get the total number of elements from the two sets.
#create a set with five integers
my_set1={23,45,43,22,34}
#create a set with five strings
my_set2={"php","python","java","html/css","IOT","cloud"}
#get the length of the two sets.
print(len(my_set1),len(my_set2))
Output:
5 6
We can also get the count using a for loop.
Example:
#create a set with five integers
my_set1={23,45,43,22,34}
#create a set with five strings
my_set2={"php","python","java","html/css","IOT","cloud"}
count=0
count1=0
#get the length of the set1
for i in my_set1:
count+=1
#display count
print(count)
#get the length of the set2
for i in my_set2:
count1+=1
#display count
print(count1)
Output:
5 6
Remove an item from a set
We can remove the element from the set using the remove() function.
Syntax:
my_set.remove("element")
Where my_set is the input set.
Example: In this example, we will remove 22 from set1 and “php” from set2.
#create a set with five integers
my_set1={23,45,43,22,34}
#create a set with five strings
my_set2={"php","python","java","html/css","IOT"}
#remove 22 from set1
my_set1.remove(22)
#remove "php" from set2
my_set2.remove("php")
#display
print(my_set1,my_set2)
Output:
{34, 43, 45, 23} {'python', 'java', 'IOT', 'html/css'}
We can also use the discard() method to remove an item from the set.
Syntax:
my_set.discard("element")
Where my_set is the input set.
Example: In this example, we will remove 22 from set1 and “php” from set2.
#create a set with five integers
my_set1={23,45,43,22,34}
#create a set with five strings
my_set2={"php","python","java","html/css","IOT"}
#remove 22 from set1
my_set1.discard(22)
#remove "php" from set2
my_set2.discard("php")
#display
print(my_set1,my_set2)
Output:
{34, 43, 45, 23} {'python', 'java', 'IOT', 'html/css'}
We can also use the pop() method to remove an item from the set. It will remove the random item from the set.
Syntax:
my_set.pop()
Where my_set is the input set.
Example: In this example, we will use the pop() method to remove the item from the set.
#create a set with five integers
my_set1={23,45,43,22,34}
#create a set with five strings
my_set2={"php","python","java","html/css","IOT"}
#remove an element from set1
my_set1.pop()
#remove an element from set2
my_set2.pop()
#display sets
print(my_set1,my_set2)
Output:
{43, 45, 22, 23} {'java', 'IOT', 'html/css', 'php'}
Also Read: How to Reverse an Array In Python [Flip Array]
Join two sets
We can join the two sets in Python using the union() function. It will get all the unique elements from both sets.
Syntax:
my_set1.union(my_set2)
where,
- my_set1 is the first set
- my_set2 is the second set
Example: In this example, we will join the integer set and the string set.
#create a set with five integers
my_set1={23,45,43,22,34}
#create a set with five strings
my_set2={"php","python","java","html/css","IOT"}
#join the two sets
print(my_set1.union(my_set2))
Output:
{34, 'php', 'python', 'java', 43, 45, 'IOT', 'html/css', 22, 23}
The intersection () is used to retrieve all the common elements between two sets.
Syntax:
my_set1.intersection(my_set2)
where,
- my_set1 is the first set
- my_set2 is the second set
Example: This example demonstrates an intersection() operation that joins two sets.
#create a set with 8 elements
my_set1={23,45,43,22,34,"java","html/css","IOT"}
#create a set with 8 elements
my_set2={"php","python","java","html/css","IOT",43,22,34}
#join the two sets with intersection
print(my_set1.intersection(my_set2))
Output:
{34, 'java', 43, 'IOT', 'html/css', 22}
Also Read: How to Check If Dict Has Key in Python [5 Methods]
Python Set Methods
clear()
This method is used to remove all the elements from the set.
Syntax:
my_set.clear()
Example:
This example will clear the elements from set1 and set2 and display the empty sets.
#create a set with 8 elements
my_set1={23,45,43,22,34,"java","html/css","IOT"}
#create a set with 8 elements
my_set2={"php","python","java","html/css","IOT",43,22,34}
#clear the elements from set1 and set2
my_set1.clear()
my_set2.clear()
print(my_set1,my_set2)
Output:
set() set()
Also Read: How to Convert Binary to Decimal in Python [5 Methods]
copy()
This method is used to copy all the elements from one set to another set.
Syntax:
my_set2=my_set1.copy()
Example:
We will copy the elements from set1 to set2 and display the copied set in this example.
#create a set with 8 elements
my_set1={23,45,43,22,34,"java","html/css","IOT"}
#copy set1
my_set2=my_set1.copy()
#display set2
print(my_set2)
Output:
{34, 'java', 43, 45, 'IOT', 'html/css', 22, 23}
difference()
This method retrieves all elements from set1 that are not present in set2.
Syntax:
my_set1.difference(my_set2)
Example:
In this example, we will apply the difference() method to both sets.
#create a set with 8 elements
my_set1={23,45,43,22,34,"java","html/css","IOT"}
#create a set with 8 elements
my_set2={"php","python","java","html/css","IOT",43,22,34}
#get the elements in set1 that are not present in set2
print(my_set1.difference(my_set2))
#get the elements in set2 that are not present in set1
print(my_set2.difference(my_set1))
Output:
{45, 23}
{'python', 'php'}
issubset()
This method is used to check whether the first set is a subset of the second set. If all the elements in the first set are present in the second set, it returns True; otherwise, False.
Syntax:
my_set1.issubset(my_set2)
where,
- my_set1 is the first set
- my_set is the second set
Example:
Check whether set1 is a subset of set2 and set2 is a subset of set1.
#create a set with five integers
my_set1={23,45,43,22,34}
#create a set with ten elements
my_set2={"php","python","java","html/css","IOT",23,45,43,22,34}
#check the set1 is the subset of set2
print(my_set1.issubset(my_set2))
#check the set2 is the subset of set1
print(my_set2.issubset(my_set1))
Output:
True False
Also Read: How To Automate Google Search With Python
issuperset()
This method is used to check whether the first set is a superset of the second set.
Syntax:
my_set1.issuperset(my_set2)
where,
- my_set1 is the first set
- my_set is the second set
Example:
Check whether set1 is the superset of set2 and set2 is the superset of set1.
#create a set with five integers
my_set1={23,45,43,22,34}
#create a set with ten elements
my_set2={"php","python","java","html/css","IOT",23,45,43,22,34}
#check the set1 is the subset of set2
print(my_set1.issuperset(my_set2))
#check the set2 is the subset of set1
print(my_set2.issuperset(my_set1))
Output:
False True
isdisjoint()
This method is used to identify common elements between the two sets. If there are no common elements in the two sets, it will return true; otherwise, it will return false.
Syntax:
my_set1.isdisjoint(my_set2)
where,
- my_set1 is the first set
- my_set is the second set
Example 1:
Check whether the two sets are disjoint.
#create a set with five integers
my_set1={23,45,43,22,34}
#create a set with five elements
my_set2={"php","python","java","html/css","IOT"}
#apply isdisjoint()
print(my_set1.isdisjoint(my_set2))
Output:
True
Example 2:
Check whether the two sets are disjoint.
#create a set with five integers
my_set1={23,45,43,22,34}
#create a set with seven elements
my_set2={"php","python","java","html/css","IOT",22,34}
#apply isdisjoint()
print(my_set1.isdisjoint(my_set2))
Output:
False
symmetric_difference()
This method returns the elements from the first set that are not present in the second set, as well as the elements from the second set that are not present in the first set.
Syntax:
my_set1.symmetric_difference(my_set2)
where,
- my_set1 is the first set
- my_set is the second set
Example :
Perform the symmetric_difference() method on the two sets.
#create a set with five integers
my_set1={23,45,43,22,34}
#create a set with seven elements
my_set2={"php","python","java","html/css","IOT",22,34}
#apply symmetric_difference()
print(my_set1.symmetric_difference(my_set2))
Output:
{'python', 'html/css', 23, 'java', 43, 45, 'IOT', 'php'}
Summary
In this article, we discuss how to create a Python empty set and perform operations such as adding, updating, removing, and retrieving the length of the set.
To join the two sets, we used the union() and intersection() methods, and we also discussed other methods, such as clear(), isdisjoint(), and symmetric_difference().
