Python 3 reference · Set Methods

Python Set add() Method

Add one element to the set. Adding an element that is already present does nothing.

Python 3 Since 3.0

Syntax

set.add(elem)

Parameters

ParameterDescription
elem A hashable value.

Return value

None.

Examples

Add elementsTry it yourself
seen = {"a"}
seen.add("b")
seen.add("a")
print(sorted(seen))

Output

['a', 'b']
Track what you've seenTry it yourself
visited = set()
for page in ["home", "about", "home"]:
    if page in visited:
        print("already saw", page)
    visited.add(page)

Output

already saw home

Version history

VersionChange
Python 3.0Added