Method | Description |
---|---|
add() | To add a new item to the set. |
clear() | This method will delete all the elements from the set. |
copy() | Creates a new copy of the set. |
difference() | Returns the item that is unique to the first set. |
discard() | Removes the item from the set. |
intersection() | Returns the set of items that are common to both sets. |
issubset() | Checks if set ‘a’ contains all the items of set ‘b’. |
issuperset() | Checks if set ‘b’ contains all the items of set ‘a’. |
pop() | To remove am item from the set. |
remove() | Removes the specified item from the set. |
union() | It returns the combined set of unique items of the sets. |
update() | It updates set ‘a’ with unique values of set ‘b’. |
Python Set add()
Method
To add a new item to the set. If the item is already available in the set, then it will not be added.
Adding new item ‘6’ to the set.
a = {1,2,3,4,4,4,4,5,5}
a.add(6)
a
Again trying to add 6, but his time as itis already availabe, so it will not be added.
a.add(6)
a
Python Set clear()
Method
This method will delete all the elements from the set.
name = {'Peter', 'Woods'}
name
Now name is just an empty set.
name.clear()
name
Python Set copy()
Method
Creates a new copy of the set.
name = {'Peter', 'Woods'}
name
name_copy = name.copy()
name_copy
We can create a copy of the set without the copy() function by directly assigning a set to a variable.
name_new_copy = name
name_new_copy
Python Set difference()
Method
Returns the item that is unique to the first set.
We have two sets with car brands in India and USA.
brands_India = {'Audi','BMW','Suzuki'}
brands_USA = {'Tesla','Audi','BMW'}
Brands only in India.
brands_India.difference(brands_USA)
Brands only in USA.
brands_USA.difference(brands_India)
Python Set discard()
Method
Removes the item from the set.
name = {'Peter', 'Woods'}
name
name.discard('Woods')
name
If the item is not found in the set, it will return the set as it is.
name = {'Peter', 'Woods'}
name.discard('John')
name
Python Set intersection()
Method
Returns the set of items that are common to both sets.
We have two sets with car brands in India and USA.
brands_India = {'Audi','BMW','Suzuki'}
brands_USA = {'Tesla','Audi','BMW'}
Brands common to both countries.
brands_India.intersection(brands_USA)
Python Set issubset()
Method
Checks if set ‘a’ contains all the items of set ‘b’.
brands_India = {'Audi','BMW'}
brands_USA = {'Tesla','Audi','BMW'}
Check if all the brands available in India are available in USA.
brands_India.issubset(brands_USA)
Python Set issuperset()
Method
Checks if set ‘b’ contains all the items of set ‘a’.
brands_India = {'Audi','BMW'}
brands_USA = {'Tesla','Audi','BMW'}
If brans set for USA contains all the brands available in India.
brands_USA.issuperset(brands_India)
Python Set pop()
Method
To remove am item from the set.
a = {1,1,2,3,4,4,4,4,5,5}
a.pop()
a
Python Set remove()
Method
Removes the specified item from the set.
cars_India = {'Audi','BMW','Ford'}
Removed the specified item from set.
cars_India.remove('Ford')
cars_India
Returns error if item is not found.
cars_India.remove('Suzuki')
Python Set union()
Method
It returns the combined set of unique items of the sets. We can pass more than one set as an argument to get the union of more than two sets.
Car brands of three countries.
cars_India = {'Audi','BMW', 'Suzuki'}
cars_USA = {'Tesla','Audi','Ford'}
cars_Canada = {'Toyata','Porsche','Tesla'}
Car brands of India and Canada.
cars_India.union(cars_Canada)
Car brands of all three countries(union with more than 1 argument).
cars_India.union(cars_Canada,cars_USA)
Python Set update()
Method
It updates set ‘a’ with unique values of set ‘b’.
cars_India = {'Audi','BMW', 'Suzuki'}
cars_USA = {'Tesla','Audi','Ford'}
cars_Canada = {'Toyata','Porsche','Tesla'}
Now cars_USA contains unique cars available in cars_Canada also.
cars_USA.update(cars_Canada)
cars_USA