Add and Remove Items from Set
flowchart TD
A["remove(x)"] --> B{"present?"}
B -->|yes| C["removed"]
B -->|no| D["KeyError"]
E["discard(x)"] --> F{"present?"}
F -->|yes| C
F -->|no| G["nothing happens, returns None"]
H["pop()"] --> I["removes an ARBITRARY element"]
I --> J["takes no argument -- you cannot choose"]
J --> K["KeyError if the set is empty"]
D --> L["use remove when absence is a bug"]
G --> M["use discard when absence is fine"]
Python Set Operations
Section titled “Python Set Operations”Python provides different methods and operators to perform set operations. In this tutorial, we will learn how to perform set operations in Python.
Add Items to Set
Section titled “Add Items to Set”Adding items to the set is very easy. We can use items using two methods. We can use the add() method to add a single item to the set. We can use the update() method to add multiple items to the set.
add() Method
Section titled “add() Method”We can use the add() method to add a single item to the set. The add() method takes a single argument and adds it to the set. The add() method does not return any value. The add() method does not add an item to the set if it is already present in the set.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data.add('h')
print(data)Output:
C:\Users\username>python set_add.py
{'a', 'e', 'b', 'd', 'g', 'h', 'f', 'c'}In this example, we declare a set and assign it to the variable data. We then add an item to the set using the add() method. The output shows that the item is added to the set.
update() Method
Section titled “update() Method”We can use the update() method to add multiple items to the set. The update() method takes a single argument and adds it to the set. The update() method does not return any value. The update() method does not add an item to the set if it is already present in the set.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'h', 'i', 'j', 'k', 'l', 'm', 'n'}
data.update(data2)
print(data)Output:
C:\Users\username>python set_update.py
{'a', 'e', 'd', 'g', 'l', 'j', 'n', 'm', 'b', 'i', 'h', 'f', 'k', 'c'}In this example, we declare two sets and assign them to the variables data and data2. We then add the items of the data2 set to the data set using the update() method. The output shows that the items are added to the set.
Add Items from Another Data Type
Section titled “Add Items from Another Data Type”Another way to add multiple items from another data type to the set is to use the update() method with another data type. We can pass a list, tuple, dictionary, or another set to the update() method to add multiple items to the set.
Add Items from List
Section titled “Add Items from List”We can pass a list to the update() method to add multiple items from the list to the set.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = ['h', 'i', 'j', 'k', 'l', 'm', 'n']
data.update(data2)
print(data)Output:
C:\Users\username>python set_update.py
{'a', 'g', 'l', 'j', 'n', 'm', 'b', 'f', 'k', 'e', 'd', 'i', 'h', 'c'}In this example, we declare a set and assign it to the variable data. We then declare a list and assign it to the variable data2. We then add the items of the data2 list to the data set using the update() method. The output shows that the items are added to the set.
Add Items from Tuple
Section titled “Add Items from Tuple”We can pass a tuple to the update() method to add multiple items from the tuple to the set.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = ('h', 'i', 'j', 'k', 'l', 'm', 'n')
data.update(data2)
print(data)Output:
C:\Users\username>python set_update.py
{'a', 'g', 'l', 'j', 'n', 'm', 'b', 'f', 'k', 'e', 'd', 'i', 'h', 'c'}In this example, we declare a set and assign it to the variable data. We then declare a tuple and assign it to the variable data2. We then add the items of the data2 tuple to the data set using the update() method. The output shows that the items are added to the set.
Add Items from String
Section titled “Add Items from String”We can pass a string to the update() method to add multiple items from the string to the set.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = 'hijklmn'
data.update(data2)
print(data)Output:
C:\Users\username>python set_update.py
{'a', 'g', 'l', 'j', 'n', 'm', 'b', 'f', 'k', 'e', 'd', 'i', 'h', 'c'}In this example, we declare a set and assign it to the variable data. We then declare a string and assign it to the variable data2. We then add the items of the data2 string to the data set using the update() method. The output shows that the items are added to the set.
union() Method
Section titled “union() Method”We can use the union() method to add multiple items to the set. The union() method takes a single argument and adds it to the set. The union() method does not return any value. The union() method does not add an item to the set if it is already present in the set.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'h', 'i', 'j', 'k', 'l', 'm', 'n'}
print(data.union(data2))
print(data)Output:
C:\Users\username>python set_union.py
{'a', 'e', 'd', 'g', 'l', 'j', 'n', 'm', 'b', 'i', 'h', 'f', 'k', 'c'}
{'a', 'e', 'b', 'd', 'g', 'f', 'c'}In this example, we declare two sets and assign them to the variables data and data2. We then add the items of the data2 set to the data set using the union() method. The output shows that the items are added to the set. The union() method does not change the original set.
union() Method with Another Data Type
Section titled “union() Method with Another Data Type”Another way to add multiple items from another data type to the set is to use the union() method with another data type. We can pass a list, tuple, dictionary, or another set to the union() method to add multiple items to the set.
union() Method with List
Section titled “union() Method with List”We can pass a list to the union() method to add multiple items from the list to the set.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = ['h', 'i', 'j', 'k', 'l', 'm', 'n']
print(data.union(data2))
print(data)Output:
C:\Users\username>python set_union.py
{'a', 'e', 'd', 'g', 'l', 'j', 'n', 'm', 'b', 'i', 'h', 'f', 'k', 'c'}
{'a', 'e', 'b', 'd', 'g', 'f', 'c'}In this example, we declare a set and assign it to the variable data. We then declare a list and assign it to the variable data2. We then add the items of the data2 list to the data set using the union() method. The output shows that the items are added to the set. The union() method does not change the original set.
union() Method with Tuple
Section titled “union() Method with Tuple”We can pass a tuple to the union() method to add multiple items from the tuple to the set.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = ('h', 'i', 'j', 'k', 'l', 'm', 'n')
print(data.union(data2))
print(data)Output:
C:\Users\username>python set_union.py
{'a', 'e', 'd', 'g', 'l', 'j', 'n', 'm', 'b', 'i', 'h', 'f', 'k', 'c'}
{'a', 'e', 'b', 'd', 'g', 'f', 'c'}In this example, we declare a set and assign it to the variable data. We then declare a tuple and assign it to the variable data2. We then add the items of the data2 tuple to the data set using the union() method. The output shows that the items are added to the set. The union() method does not change the original set.
union() Method with String
Section titled “union() Method with String”We can pass a string to the union() method to add multiple items from the string to the set.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = 'hijklmn'
print(data.union(data2))
print(data)Output:
C:\Users\username>python set_union.py
{'a', 'e', 'd', 'g', 'l', 'j', 'n', 'm', 'b', 'i', 'h', 'f', 'k', 'c'}
{'a', 'e', 'b', 'd', 'g', 'f', 'c'}In this example, we declare a set and assign it to the variable data. We then declare a string and assign it to the variable data2. We then add the items of the data2 string to the data set using the union() method. The output shows that the items are added to the set. The union() method does not change the original set.
| Operator
Section titled “| Operator”We can use the | operator to add multiple items to the set. The | operator takes a single argument and adds it to the set. The | operator does not return any value. The | operator does not add an item to the set if it is already present in the set.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'h', 'i', 'j', 'k', 'l', 'm', 'n'}
print(data | data2)
print(data)Output:
C:\Users\username>python set_union.py
{'a', 'e', 'd', 'g', 'l', 'j', 'n', 'm', 'b', 'i', 'h', 'f', 'k', 'c'}
{'a', 'e', 'b', 'd', 'g', 'f', 'c'}In this example, we declare two sets and assign them to the variables data and data2. We then add the items of the data2 set to the data set using the | operator. The output shows that the items are added to the set. The | operator does not change the original set.
Remove Items from Set
Section titled “Remove Items from Set”Removing items from the set is very easy. We can use items using two methods. We can use the remove() method to remove a single item from the set. We can use the discard() method to remove multiple items from the set.
remove() Method
Section titled “remove() Method”We can use the remove() method to remove a single item from the set. The remove() method takes a single argument and removes it from the set. The remove() method does not return any value. The remove() method raises an error if the item is not present in the set.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data.remove('f')
print(data)Output:
C:\Users\username>python set_remove.py
{'a', 'e', 'b', 'd', 'g', 'c'}In this example, we declare a set and assign it to the variable data. We then remove an item from the set using the remove() method. The output shows that the item is removed from the set.
discard() Method
Section titled “discard() Method”We can use the discard() method to remove multiple items from the set. The discard() method takes a single argument and removes it from the set. The discard() method does not return any value. The discard() method does not raise an error if the item is not present in the set.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data.discard('f')
print(data)
data.discard('h')
print(data)Output:
C:\Users\username>python set_discard.py
{'a', 'e', 'b', 'd', 'g', 'c'}
{'a', 'e', 'b', 'd', 'g', 'c'}In this example, we declare a set and assign it to the variable data. We then remove an item from the set using the discard() method. The output shows that the item is removed from the set. We then remove an item from the set using the discard() method. The output shows that the item is not removed from the set.
pop() Method
Section titled “pop() Method”We can use the pop() method to remove an item from the set. The pop() method does not take any argument and removes an item from the set. The pop() method returns the removed item. The pop() method removes the last item from the set.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
print(data.pop())
print(data)Output:
C:\Users\username>python set_pop.py
a
{'e', 'b', 'd', 'g', 'f', 'c'}In this example, we declare a set and assign it to the variable data. We then remove an item from the set using the pop() method. The output shows that the item is removed from the set. The pop() method removes the last item from the set.
clear() Method
Section titled “clear() Method”We can use the clear() method to remove all items from the set. The clear() method does not take any argument and removes all items from the set. The clear() method does not return any value.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data.clear()
print(data)Output:
C:\Users\username>python set_clear.py
set()In this example, we declare a set and assign it to the variable data. We then remove all items from the set using the clear() method. The output shows that the items are removed from the set.
del Keyword
Section titled “del Keyword”We can use the del keyword to remove the set. The del keyword does not take any argument and removes the set. The del keyword does not return any value.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
del data
print(data)Output:
C:\Users\username>python set_del.py
Traceback (most recent call last):
File "set_del.py", line 3, in <module>
print(data)
NameError: name 'data' is not definedIn this example, we declare a set and assign it to the variable data. We then remove the set using the del keyword. The output shows that the set is removed. The del keyword does not return any value.
difference_update() Method
Section titled “difference_update() Method”We can use the difference_update() method to remove multiple items from the set. The difference_update() method takes a single argument and removes it from the set. The difference_update() method does not return any value. The difference_update() method does not raise an error if the item is not present in the set.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'d', 'e', 'f', 'g', 'h', 'i', 'j'}
data.difference_update(data2)
print(data)Output:
C:\Users\username>python set_difference_update.py
{'a', 'b', 'c'}In this example, we declare two sets and assign them to the variables data and data2. We then remove the items of the data2 set from the data set using the difference_update() method. The output shows that the items are removed from the set. The difference_update() method does change the original set.
difference() Method
Section titled “difference() Method”We can use the difference() method to remove multiple items from the set. The difference() method takes a single argument and removes it from the set. The difference() method does not return any value. The difference() method does not raise an error if the item is not present in the set.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'d', 'e', 'f', 'g', 'h', 'i', 'j'}
print(data.difference(data2))
print(data)Output:
C:\Users\username>python set_difference.py
{'a', 'b', 'c'}
{'a', 'e', 'b', 'd', 'g', 'f', 'c'}In this example, we declare two sets and assign them to the variables data and data2. We then remove the items of the data2 set from the data set using the difference() method. The output shows that the items are removed from the set. The difference() method does not change the original set.
intersection_update() Method
Section titled “intersection_update() Method”We can use the intersection_update() method to remove multiple items from the set. The intersection_update() method takes a single argument and removes it from the set. The intersection_update() method does not return any value. The intersection_update() method does not raise an error if the item is not present in the set.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'d', 'e', 'f', 'g', 'h', 'i', 'j'}
data.intersection_update(data2)
print(data)Output:
C:\Users\username>python set_intersection_update.py
{'e', 'd', 'g', 'f'}In this example, we declare two sets and assign them to the variables data and data2. We then remove the items of the data2 set from the data set using the intersection_update() method. The output shows that the items are removed from the set. The intersection_update() method does change the original set.
intersection() Method
Section titled “intersection() Method”We can use the intersection() method to remove multiple items from the set. The intersection() method takes a single argument and removes it from the set. The intersection() method does not return any value. The intersection() method does not raise an error if the item is not present in the set.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'d', 'e', 'f', 'g', 'h', 'i', 'j'}
print(data.intersection(data2))
print(data)Output:
C:\Users\username>python set_intersection.py
{'e', 'd', 'g', 'f'}
{'a', 'e', 'b', 'd', 'g', 'f', 'c'}In this example, we declare two sets and assign them to the variables data and data2. We then remove the items of the data2 set from the data set using the intersection() method. The output shows that the items are removed from the set. The intersection() method does not change the original set.
symmetric_difference_update() Method
Section titled “symmetric_difference_update() Method”We can use the symmetric_difference_update() method to remove multiple items from the set. The symmetric_difference_update() method takes a single argument and removes it from the set. The symmetric_difference_update() method does not return any value. The symmetric_difference_update() method does not raise an error if the item is not present in the set.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'d', 'e', 'f', 'g', 'h', 'i', 'j'}
data.symmetric_difference_update(data2)
print(data)Output:
C:\Users\username>python set_symmetric_difference_update.py
{'a', 'j', 'b', 'i', 'h', 'c'}In this example, we declare two sets and assign them to the variables data and data2. We then remove the items of the data2 set from the data set using the symmetric_difference_update() method. The output shows that the items are removed from the set. The symmetric_difference_update() method does change the original set.
symmetric_difference() Method
Section titled “symmetric_difference() Method”We can use the symmetric_difference() method to remove multiple items from the set. The symmetric_difference() method takes a single argument and removes it from the set. The symmetric_difference() method does not return any value. The symmetric_difference() method does not raise an error if the item is not present in the set.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'d', 'e', 'f', 'g', 'h', 'i', 'j'}
print(data.symmetric_difference(data2))
print(data)Output:
C:\Users\username>python set_symmetric_difference.py
{'a', 'j', 'b', 'i', 'h', 'c'}
{'a', 'e', 'b', 'd', 'g', 'f', 'c'}In this example, we declare two sets and assign them to the variables data and data2. We then remove the items of the data2 set from the data set using the symmetric_difference() method. The output shows that the items are removed from the set. The symmetric_difference() method does not change the original set.
& Operator
Section titled “& Operator”We can use the & operator to remove multiple items from the set. The & operator takes a single argument and removes it from the set. The & operator does not return any value. The & operator does not raise an error if the item is not present in the set.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
print(data & {'d', 'e', 'f', 'g', 'h', 'i', 'j'})
print(data)Output:
C:\Users\username>python set_symmetric_difference.py
{'e', 'd', 'g', 'f'}
{'a', 'e', 'b', 'd', 'g', 'f', 'c'}In this example, we declare a set and assign it to the variable data. We then remove the items of the data2 set from the data set using the & operator. The output shows that the items are removed from the set. The & operator does not change the original set.
Conclusion
Section titled “Conclusion”In this tutorial, we have learned about the Python set. We have learned how to create a set. We have learned how to add items to the set. We have learned how to remove items from the set. We have learned how to use different methods to add and remove items from the set. We have learned how to use different operators to add and remove items from the set. For more tutorials on Python, please visit our Python Central Hub.
Check yourself
Section titled “Check yourself”-
`s = {1,2,3}`. What does `s.remove(99)` do?
Verified. `remove` treats absence as an error; `discard` does not. Choose by whether a miss means a bug.
pch.quizShowAnswer
C — Raises `KeyError` — Verified. `remove` treats absence as an error; `discard` does not. Choose by whether a miss means a bug.
-
What does `s.discard(99)` return when 99 is not present?
It returns None and changes nothing. Useful when the element may legitimately not be there.
pch.quizShowAnswer
B — `None` — It returns None and changes nothing. Useful when the element may legitimately not be there.
-
Which element does `s.pop()` remove?
A set has no order to appeal to, so `pop` takes no argument and gives you whichever element it reaches first. It raises `KeyError` on an empty set.
pch.quizShowAnswer
C — An arbitrary one — it takes no argument — A set has no order to appeal to, so `pop` takes no argument and gives you whichever element it reaches first. It raises `KeyError` on an empty set.
-
You are removing IDs supplied by a user from a set of known IDs. Which method fits?
Input you do not control will contain values that are not there. `discard` treats that as ordinary; `remove` would turn it into an exception on the happy path.
pch.quizShowAnswer
B — `discard`, because an unknown ID is expected input — Input you do not control will contain values that are not there. `discard` treats that as ordinary; `remove` would turn it into an exception on the happy path.
Try it: Add and Remove in Set Exercises
Section titled “Try it: Add and Remove in Set Exercises”Exercise 1 – add()
Section titled “Exercise 1 – add()”Exercise 2 – remove() and discard()
Section titled “Exercise 2 – remove() and discard()”Exercise 3 – update()
Section titled “Exercise 3 – update()”pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading