.add()
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 setset_1={1,2,4,5}# Using .add() method to add a new unique element to set_1set_1.add(3)# Print the set with the additional element.print(set_1)# Using .add() method to add an existing element 2 to set_1set_1.add(2)# Print the set with no changesprint(set_1)
The code example above will produce the following output:
{1, 2, 3, 4, 5}{1, 2, 3, 4, 5}
Codebyte Example
Note: To create an empty set, use
set()
instead of{}
, as the latter creates an empty dictionary.
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.