How to Create Python Empty Set

162

This tutorial will discuss how to create a Python empty set and different operations performed on the set. In Python, a set is a data structure that will store unique elements. It will not allow duplicates. Set is represented by {}.

Example: my_set={1,2,3,4,5}

Also ReadPython Matrix Using Numpy [With Examples]

How to Create Python Empty Set

Let’s first see how you can create Python empty set. We can create an empty set by using the set() function.

Syntax:

set()

If we want 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'>

Also ReadHow To Create An Empty Dictionary in Python

Access items in a set

If we want to access the elements in a set, we can use for loop to iterate 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 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'}

Also ReadHow to Exit Python Program [4 Methods]

Add items to a Set

We can add elements to the set by using 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: We will add 22 to the first set and “php” to the second set in this 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"}

#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'}

Also ReadHow to Convert Python Tuples to Lists

Update set

We can also update all existing elements in the set using the update() function.

Syntax:

my_set.update(["elements"])

where,

  1. my_set is the input set
  2. 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'}

Also ReadHow to Handle String Index Out of Range Error In Python

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 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

Also ReadPython For Loop Index With Examples

Remove item from a set

We can remove the element from the set by 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:

We will use the pop() method to remove the item from the set in this 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"}

#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 ReadHow 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,

  1. my_set1 is the first set
  2. my_set2 is the second set

Example:

In this example, we will join the integer set and 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}

intersection() is used to get all the common elements from both sets.

Syntax:

my_set1.intersection(my_set2)

where,

  1. my_set1 is the first set
  2. my_set2 is the second set

Example:

This example will perform an intersection() operation to join 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 ReadHow 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 ReadHow 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}

Also ReadPython Program To Display Fibonacci Series Up To N Terms

difference()

This method will get all the elements from set1 that are not present in set2.

Syntax:

my_set1.difference(my_set2)

Example:

We will apply the difference() method to both sets in this example.

#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'}

Also ReadPython Program To Display Multiplication Table of Any Number

issubset()

This method is used to check the first set is the 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,

  1. my_set1 is the first set
  2. my_set is the second set

Example:

Check whether set1 is the subset of set2  and set2 is the 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 ReadHow To Automate Google Search With Python

issuperset()

This method is used to check the first set is the superset of the second set.

Syntax:

my_set1.issuperset(my_set2)

where,

  1. my_set1 is the first set
  2. 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

Also readPython App Development Scope—Everything You Should Know

isdisjoint()

This method is used to check the common elements in the two sets. If there are no common elements in the two sets, it will return true; otherwise, it is false.

Syntax:

my_set1.isdisjoint(my_set2)

where,

  1. my_set1 is the first set
  2. my_set is the second set

Example 1:

Check whether the two sets are disjoint or not.

#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 or not.

#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

Also Read10 Best Udemy Python Courses for Beginners

symmetric_difference()

This method will return the elements from the first set that are not present in the second set and return the elements from the second set that are not present in the first set.

Syntax:

my_set1.symmetric_difference(my_set2)

where,

  1. my_set1 is the first set
  2. 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 discussed how to create Python empty set and add, update, remove and get the length of the set. To join the two sets we used union() and intersection() methods and we discussed other methods like clear(),isdisjoint(),symmetric_difference() methods.