Python Set add() Method
Description
The add() method adds an element to a set; if the element already exists in the set, nothing is done.
Syntax
Syntax of the add() method:
set.add(elmnt)
Parameters
- elmnt -- required, the element to add.
Return Value
none.
Example
The following example demonstrates the use of the add() method:
Example 1
fruits
= {
"
apple
"
,
"
banana
"
,
"
cherry
"
}
fruits
.
add
(
"
orange
"
)
(
fruits
)
The output is:
{'apple', 'banana', 'orange', 'cherry'}
For an element that already exists, no addition is performed:
Example 2
fruits
= {
"
apple
"
,
"
banana
"
,
"
cherry
"
}
fruits
.
add
(
"
apple
"
)
(
fruits
)
The output is:
{'apple', 'banana', 'cherry'}
Python Sets