How does set add() work in Python?

The built-in add() method in Python is used to add an element to a set.

Syntax

set.add(element)

Parameters and return value

This method requires one element to be passed as a parameter. If that element already exists in the set, it won’t be added.

It is important to note that this method only returns None. Instead of returning a value, it adds an element to the set that calls the method.

Example

x = {'Cats', 'Dogs', 'Elephants', 'Seals'}
x.add('Tiger')
print(x)

Free Resources