Home / Python / Reference / Set methods

Python set add() method

Add one item to a set.

Syntax

set.add(item)

Adding an item that is already there does nothing.

Parameters

itemthe value to add

Return value

None

Example

Code

seen = set()
for x in [3, 1, 3, 2, 1]:
    seen.add(x)
print(seen, len(seen))

Output

{1, 2, 3} 3

Related

Learn it step by step in the free lesson Dictionaries & Sets, with exercises that check your answers.