Skip to content
Set Methods

add()

Add a single element to the set.

set.add(element)

set.add() adds element to the set if it is not already present.

Because sets maintain uniqueness, adding an existing element has no effect and does not raise an error.

Parameters

NameDescription
elementThe hashable object to add to the set.

Returns

None — the set is mutated in place.

Try it

Run it and change it
tags = {"python", "coding"}
tags.add("backend")
tags.add("python")
print(sorted(tags))

Worth knowing

  • Hashable items only: Set elements must be immutable/hashable (strings, numbers, tuples). Adding a list or dictionary raises TypeError.
  • Adding multiple items: Use update() to add multiple elements from an iterable at once.

Related entries