Python -Add Set Items
Add Items
Once a set is created, you cannot change its items, but you can add new items.
To add one item to a set use theadd() method.
Example
Add an item to a set, using theadd() method:
thisset = {"apple", "banana", "cherry"}
thisset.add("orange")
print(thisset)
Try it Yourself »thisset.add("orange")
print(thisset)
Add Sets
To add items from another set into the current set, use theupdate() method.
Example
Add elements fromtropical intothisset:
thisset = {"apple", "banana", "cherry"}
tropical = {"pineapple", "mango", "papaya"}
thisset.update(tropical)
print(thisset)
Try it Yourself »tropical = {"pineapple", "mango", "papaya"}
thisset.update(tropical)
print(thisset)
Add Any Iterable
The object in theupdate() method does not have to be a set, it can be any iterable object (tuples, lists, dictionaries etc.).
Example
Add elements of a list to at set:
thisset = {"apple", "banana", "cherry"}
mylist = ["kiwi", "orange"]
thisset.update(mylist)
print(thisset)
Try it Yourself »mylist = ["kiwi", "orange"]
thisset.update(mylist)
print(thisset)

