Python Set add() Method

❮ Set Methods


Example

Add an element to the fruits set:

fruits = {"apple", "banana", "cherry"}

fruits.add("orange")

print(fruits)
Try it Yourself »

Definition and Usage

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

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


Syntax

set.add(elmnt)

Parameter Values

Parameter Description
elmnt Required. The element to add to the set

More Examples

Example

Try to add an element that already exists:

fruits = {"apple", "banana", "cherry"}

fruits.add("apple")

print(fruits)
Try it Yourself »

❮ Set Methods

Copyright 1999-2023 by Refsnes Data. All Rights Reserved.