Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

set.add

Inserts one hashable object into a set.

Version: Python 3.14; repository image, production runtime not attested

Syntax

s.add(element, /)

Inserts one hashable object into a set.

Inserting an already present value preserves a single representative.

The two insertions of 4 contribute a single member to the original set. Both calls return None. A list is unhashable and cannot be added as an element: attempting this raises TypeError.

s = {1}
assert s.add(True) is None and len(s) == 1

Examples

s = {2, 3}
s.add(4)
s.add(4)
print(sorted(s))

Result

[2, 3, 4]

The two insertions of 4 contribute a single member to the original set. Both calls return None. A list is unhashable and cannot be added as an element: attempting this raises TypeError.

Sources