Last modified: Mar 19, 2026 By Alexander Williams
Python Set Methods Guide: Add, Remove, Compare
Python sets are powerful. They store unordered, unique items. To use them well, you need to know their methods. This guide covers all key set methods.
We will look at adding, removing, and comparing data. Each method will have a clear example. You will learn how to manage collections efficiently.
What Are Python Sets?
A set is a built-in data type. It holds a collection of items. The items are unique and unordered. You create a set with curly braces {} or the set() function.
Sets are mutable. You can change them after creation. They are perfect for removing duplicates and membership tests. For a full introduction, see our Python Sets Guide: Unordered Unique Collections.
# Creating a set
my_set = {1, 2, 3, 4, 5}
print(my_set)
{1, 2, 3, 4, 5}
Methods for Adding Items
You cannot change individual items in a set. But you can add new ones. Use these methods to grow your set.
The add() Method
The add() method puts one item into the set. If the item is already there, nothing happens.
fruits = {"apple", "banana"}
fruits.add("orange")
print(fruits)
# Trying to add a duplicate
fruits.add("apple")
print(fruits) # Set remains unchanged
{'banana', 'orange', 'apple'}
{'banana', 'orange', 'apple'}
The update() Method
The update() method adds multiple items. You can pass lists, tuples, or other sets. It merges them into the original set.
numbers = {1, 2}
numbers.update([3, 4, 5])
print(numbers)
{1, 2, 3, 4, 5}
For more on adding items, check Python Set Insert: How to Add Items.
Methods for Removing Items
Removing items is just as important as adding them. Python provides several ways. Some raise errors if an item is missing. Others do not.
The remove() Method
The remove() method deletes a specific item. If the item is not found, it raises a KeyError. Use this when you are sure the item exists.
colors = {"red", "blue", "green"}
colors.remove("blue")
print(colors)
# colors.remove("yellow") # This would cause a KeyError
{'green', 'red'}
The discard() Method
The discard() method also removes an item. The key difference? It does not raise an error if the item is missing. This makes it safer.
colors.discard("green")
print(colors)
colors.discard("yellow") # No error, set unchanged
print(colors)
{'red'}
{'red'}
The pop() Method
The pop() method removes and returns an arbitrary item. Since sets are unordered, you cannot know which item it will be. It raises KeyError on an empty set.
item = colors.pop()
print(f"Removed: {item}")
print(f"Set now: {colors}")
Removed: red
Set now: set()
The clear() Method
The clear() method empties the entire set. It removes all items at once, leaving you with an empty set.
new_set = {10, 20, 30}
new_set.clear()
print(new_set)
set()
Set Comparison Methods
These methods let you compare two sets. They are vital for data analysis. You can find common elements, differences, and more.
The union() Method
The union() method returns a new set. It contains all items from both sets. Duplicates are automatically removed. You can also use the | operator.
set_a = {1, 2, 3}
set_b = {3, 4, 5}
union_set = set_a.union(set_b)
print(union_set)
{1, 2, 3, 4, 5}
The intersection() Method
The intersection() method finds common items. It returns a set with elements present in both sets. The & operator does the same thing. Learn more in our Python Set Intersection: Find Common Elements guide.
common_set = set_a.intersection(set_b)
print(common_set)
{3}
The difference() Method
The difference() method shows items in the first set but not in the second. Use the - operator for the same result. For a deep dive, see Python Set Difference: Find Unique Items.
diff_set = set_a.difference(set_b) # Items in A not in B
print(diff_set)
{1, 2}
The symmetric_difference() Method
The symmetric_difference() method finds items in either set, but not in both. It's like an exclusive OR. The ^ operator is its shortcut.
sym_diff = set_a.symmetric_difference(set_b)
print(sym_diff)
{1, 2, 4, 5}
Boolean Check Methods
These methods return True or False. They check the relationship between two sets. They are great for conditional logic.
issubset() and issuperset()
The issubset() method checks if all items of one set are in another. The issuperset() method checks the opposite.
small = {1, 2}
large = {1, 2, 3, 4}
print(small.issubset(large)) # True
print(large.issuperset(small)) # True
True
True
isdisjoint()
The isdisjoint() method checks if two sets have no items in common. It returns True if they are completely separate.
set_x = {1, 2}
set_y = {3, 4}
print(set_x.isdisjoint(set_y))
True
Copying a Set
Use the copy() method to create a shallow duplicate. This is important. It prevents changes to the new set from affecting the original.
original = {100, 200}
duplicate = original.copy()
duplicate.add(300)
print("Original:", original)
print("Duplicate:", duplicate)
Original: {200, 100}
Duplicate: {200, 100, 300}
Conclusion
Python set methods are essential tools. They help you manage unique collections with ease. You learned how to add, remove, and compare data.
Remember the key methods: add(), remove(), union(), intersection(), and difference(). Use discard() for safe removal. Use boolean checks for logic.
Practice with these examples. Try combining methods for complex tasks. For more advanced topics like nested sets, explore our Python Set of Sets: Nested Collections Guide. Keep your code clean and efficient with Python sets.