🛠 Set Methods
Last Updated: 17th August 2025
- add() – Adds a single element to the set.
fruits = {"apple", "banana"}
fruits.add("mango")
print(fruits) # {'apple', 'banana', 'mango'}
- update() – Adds multiple elements to the set.
fruits = {"apple", "banana"}
fruits.update(["mango", "kiwi"])
print(fruits) # {'apple', 'banana', 'mango', 'kiwi'}
- remove() – Removes a single element from the set.
fruits = {"apple", "banana", "cherry"}
fruits.remove("banana")
print(fruits) # {'apple', 'cherry'}
fruits.remove("mango") # KeyError
- discard() – Removes a single element from the set if it exists.
fruits = {"apple", "banana", "cherry"}
fruits.discard("banana")
print(fruits) # {'apple', 'cherry'}
fruits.discard("mango") # No error
- pop() – Removes and returns an arbitrary element from the set.
fruits = {"apple", "banana", "cherry"}
fruit = fruits.pop()
print(fruits) # {'apple', 'cherry'}
- intersection() – Returns a new set with elements common to both sets also use
&.
fruits1 = {"apple", "banana", "cherry"}
fruits2 = {"banana", "kiwi", "mango"}
commonFruits = fruits1.intersection(fruits2)
print(commonFruits) # {'banana'}
commonFruits = fruits1 & fruits2
print(commonFruits) # {'banana'}
- union() – Returns a new set with elements from both sets also use
|.
fruits1 = {"apple", "banana", "cherry"}
fruits2 = {"banana", "kiwi", "mango"}
allFruits = fruits1.union(fruits2)
print(allFruits) # {'apple', 'banana', 'cherry', 'kiwi', 'mango'}
allFruits = fruits1 | fruits2
print(allFruits) # {'apple', 'banana', 'cherry', 'kiwi', 'mango'}
- difference() – Returns a new set with elements in the first set but not in the second set also use
-.
fruits1 = {"apple", "banana", "cherry"}
fruits2 = {"banana", "kiwi", "mango"}
diffFruits = fruits1.difference(fruits2)
print(diffFruits) # {'apple', 'cherry'}
diffFruits = fruits1 - fruits2
print(diffFruits) # {'apple', 'cherry'}
- symmetric_difference() – Returns a new set with elements in either the first set or the second set but not in both also use
^.
fruits1 = {"apple", "banana", "cherry"}
fruits2 = {"banana", "kiwi", "mango"}
symDiffFruits = fruits1.symmetric_difference(fruits2)
print(symDiffFruits) # {'apple', 'cherry', 'kiwi', 'mango'}
symDiffFruits = fruits1 ^ fruits2
print(symDiffFruits) # {'apple', 'cherry', 'kiwi', 'mango'}
- isdisjoint() – Returns True if two sets have a null intersection.
fruits1 = {"apple", "banana", "cherry"}
fruits2 = {"kiwi", "mango"}
isDisjoint = fruits1.isdisjoint(fruits2)
print(isDisjoint) # True
- issubset() – Returns True if another set contains this set.
fruits1 = {"apple", "banana", "cherry"}
fruits2 = {"apple", "banana"}
isSubset = fruits1.issubset(fruits2)
print(isSubset) # False
- issuperset() – Returns True if this set contains another set.
fruits1 = {"apple", "banana", "cherry"}
fruits2 = {"apple", "banana"}
isSuperset = fruits1.issuperset(fruits2)
print(isSuperset) # True
💡 Quick Practice
- Create a set of your favorite fruits.
- Add a new fruit to the set.
- Remove a fruit from the set.
- Loop through the set and print each fruit.
- Exercise