Python Collection add() Method

Example

Add an element to the fruits set:

fruits = {"apple", "banana", "cherry"}
fruits.add("orange") 
print(fruits)

Run Example

Definition and Usage

The add() method adds an element to the set.

If the element already exists, the add() method will not add the element.

Syntax

set.add(element)

Parameter Value

Parameter Description
element Required. The element to be added to the set.

More Examples

Example

Please try to add an existing element:

fruits = {"apple", "banana", "cherry"}
fruits.add("apple")
print(fruits)

Run Example