.add()

Anonymous contributor's avatar
Anonymous contributor
Published Jun 23, 2024
Contribute to Docs

In Python, the .add() method adds a specified single element to a set if it is not already present, thus maintaining its unique elements property. If the element is already present in the set, the operation has no effect. The method modifies the set in-place.

Note: If no argument is provided to .add() method, it raises an error.

Syntax

set.add(element)
  • set: The set to which an element will be added.
  • element: The element to be added to the set. Only one element can be added per method call.

Example

The following example demonstrates the use of the .add() method:

# Create a set
set_1={1,2,4,5}
# Using .add() method to add a new unique element to set_1
set_1.add(3)
# Print the set with the additional element.
print(set_1)
# Using .add() method to add an existing element 2 to set_1
set_1.add(2)
# Print the set with no changes
print(set_1)

The code example above will produce the following output:

{1, 2, 3, 4, 5}
{1, 2, 3, 4, 5}

Codebyte Example

Code
Output
Loading...

Note: To create an empty set, use set() instead of {}, as the latter creates an empty dictionary.

All contributors

Contribute to Docs

Learn Python on Codecademy