Python Set Methods


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.

In [1]:
a = {1,2,3,4,4,4,4,5,5}
a.add(6)
a
Out[1]:
{1, 2, 3, 4, 5, 6}

Again trying to add 6, but his time as itis already availabe, so it will not be added.

In [2]:
a.add(6)
a
Out[2]:
{1, 2, 3, 4, 5, 6}

Python Set clear() Method

This method will delete all the elements from the set.

In [1]:
name = {'Peter', 'Woods'}
name
Out[1]:
{'Peter', 'Woods'}

Now name is just an empty set.

In [2]:
name.clear()
name
Out[2]:
set()

Python Set copy() Method

Creates a new copy of the set.

In [1]:
name = {'Peter', 'Woods'}
name
Out[1]:
{'Peter', 'Woods'}
In [2]:
name_copy = name.copy()
name_copy
Out[2]:
{'Peter', 'Woods'}

We can create a copy of the set without the copy() function by directly assigning a set to a variable.

In [3]:
name_new_copy = name
name_new_copy
Out[3]:
{'Peter', 'Woods'}

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.

In [1]:
brands_India = {'Audi','BMW','Suzuki'}
brands_USA = {'Tesla','Audi','BMW'}

Brands only in India.

In [2]:
brands_India.difference(brands_USA)
Out[2]:
{'Suzuki'}

Brands only in USA.

In [3]:
brands_USA.difference(brands_India)
Out[3]:
{'Tesla'}

Python Set discard() Method

Removes the item from the set.

In [1]:
name = {'Peter', 'Woods'}
name
Out[1]:
{'Peter', 'Woods'}
In [2]:
name.discard('Woods')
name
Out[2]:
{'Peter'}

If the item is not found in the set, it will return the set as it is.

In [3]:
name = {'Peter', 'Woods'}
name.discard('John')
name
Out[3]:
{'Peter', 'Woods'}

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.

In [1]:
brands_India = {'Audi','BMW','Suzuki'}
brands_USA = {'Tesla','Audi','BMW'}

Brands common to both countries.

In [2]:
brands_India.intersection(brands_USA)
Out[2]:
{'Audi', 'BMW'}

Python Set issubset() Method

Checks if set ‘a’ contains all the items of set ‘b’.

In [1]:
brands_India = {'Audi','BMW'}
brands_USA = {'Tesla','Audi','BMW'}

Check if all the brands available in India are available in USA.

In [2]:
brands_India.issubset(brands_USA)
Out[2]:
True

Python Set issuperset() Method

Checks if set ‘b’ contains all the items of set ‘a’.

In [1]:
brands_India = {'Audi','BMW'}
brands_USA = {'Tesla','Audi','BMW'}

If brans set for USA contains all the brands available in India.

In [2]:
brands_USA.issuperset(brands_India)
Out[2]:
True

Python Set pop() Method

To remove am item from the set.

In [1]:
a = {1,1,2,3,4,4,4,4,5,5}
a.pop()
Out[1]:
1
In [2]:
a
Out[2]:
{2, 3, 4, 5}

Python Set remove() Method

Removes the specified item from the set.

In [1]:
cars_India = {'Audi','BMW','Ford'}

Removed the specified item from set.

In [2]:
cars_India.remove('Ford')
cars_India
Out[2]:
{'Audi', 'BMW'}

Returns error if item is not found.

In [3]:
cars_India.remove('Suzuki')
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-3-dc4ab0cd75e3> in <module>()
----> 1 cars_India.remove('Suzuki')

KeyError: '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.

In [1]:
cars_India = {'Audi','BMW', 'Suzuki'}
cars_USA = {'Tesla','Audi','Ford'}
cars_Canada = {'Toyata','Porsche','Tesla'}

Car brands of India and Canada.

In [2]:
cars_India.union(cars_Canada)
Out[2]:
{'Audi', 'BMW', 'Porsche', 'Suzuki', 'Tesla', 'Toyata'}

Car brands of all three countries(union with more than 1 argument).

In [3]:
cars_India.union(cars_Canada,cars_USA)
Out[3]:
{'Audi', 'BMW', 'Ford', 'Porsche', 'Suzuki', 'Tesla', 'Toyata'}

Python Set update() Method

It updates set ‘a’ with unique values of set ‘b’.

In [1]:
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.

In [2]:
cars_USA.update(cars_Canada)
cars_USA
Out[2]:
{'Audi', 'Ford', 'Porsche', 'Tesla', 'Toyata'}

Resources

Handwritten Notes