Skip to content

Add and Remove Items from Set

diagram three ways to take something out of a set mermaid
They differ only in what happens when the element is not there, and whether you get to choose which one leaves. Picking the wrong one is either a crash on data you did not control or a silent no-op that hides a bug.

Python provides different methods and operators to perform set operations. In this tutorial, we will learn how to perform set operations in Python.

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.

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.

set_add.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data.add('h')
print(data)

Output:

command
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.

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.

set_update.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'h', 'i', 'j', 'k', 'l', 'm', 'n'}
data.update(data2)
print(data)

Output:

command
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.

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.

We can pass a list to the update() method to add multiple items from the list to the set.

set_update.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = ['h', 'i', 'j', 'k', 'l', 'm', 'n']
data.update(data2)
print(data)

Output:

command
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.

We can pass a tuple to the update() method to add multiple items from the tuple to the set.

set_update.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = ('h', 'i', 'j', 'k', 'l', 'm', 'n')
data.update(data2)
print(data)

Output:

command
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.

We can pass a string to the update() method to add multiple items from the string to the set.

set_update.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = 'hijklmn'
data.update(data2)
print(data)

Output:

command
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.

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.

set_union.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'h', 'i', 'j', 'k', 'l', 'm', 'n'}
print(data.union(data2))
print(data)

Output:

command
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.

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.

We can pass a list to the union() method to add multiple items from the list to the set.

set_union.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = ['h', 'i', 'j', 'k', 'l', 'm', 'n']
print(data.union(data2))
print(data)

Output:

command
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.

We can pass a tuple to the union() method to add multiple items from the tuple to the set.

set_union.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = ('h', 'i', 'j', 'k', 'l', 'm', 'n')
print(data.union(data2))
print(data)

Output:

command
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.

We can pass a string to the union() method to add multiple items from the string to the set.

set_union.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = 'hijklmn'
print(data.union(data2))
print(data)

Output:

command
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.

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.

set_union.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'h', 'i', 'j', 'k', 'l', 'm', 'n'}
print(data | data2)
print(data)

Output:

command
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.

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.

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.

set_remove.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data.remove('f')
print(data)

Output:

command
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.

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.

set_discard.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data.discard('f')
print(data)
data.discard('h')
print(data)

Output:

command
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.

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.

set_pop.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
print(data.pop())
print(data)

Output:

command
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.

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.

set_clear.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data.clear()
print(data)

Output:

command
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.

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.

set_del.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
del data
print(data)

Output:

command
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 defined

In 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.

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.

set_difference_update.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'d', 'e', 'f', 'g', 'h', 'i', 'j'}
data.difference_update(data2)
print(data)

Output:

command
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.

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.

set_difference.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'d', 'e', 'f', 'g', 'h', 'i', 'j'}
print(data.difference(data2))
print(data)

Output:

command
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.

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.

set_intersection_update.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'d', 'e', 'f', 'g', 'h', 'i', 'j'}
data.intersection_update(data2)
print(data)

Output:

command
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.

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.

set_intersection.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'d', 'e', 'f', 'g', 'h', 'i', 'j'}
print(data.intersection(data2))
print(data)

Output:

command
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.

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.

set_symmetric_difference_update.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'d', 'e', 'f', 'g', 'h', 'i', 'j'}
data.symmetric_difference_update(data2)
print(data)

Output:

command
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.

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.

set_symmetric_difference.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'d', 'e', 'f', 'g', 'h', 'i', 'j'}
print(data.symmetric_difference(data2))
print(data)

Output:

command
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.

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.

set_symmetric_difference.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
print(data & {'d', 'e', 'f', 'g', 'h', 'i', 'j'})
print(data)

Output:

command
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.

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.


sketch remove, discard and pop, when the element is not there p5.js
Three ways to take something out, differing only in what happens on a miss and whether you get to choose which element leaves. Picking the wrong one is either a crash on data you did not control, or a silent no-op that hides the fact your assumption was wrong.
pch.quizTag pch.quizDefaultTitle
  1. `s = {1,2,3}`. What does `s.remove(99)` do?

    pch.quizShowAnswer

    C — Raises `KeyError` — Verified. `remove` treats absence as an error; `discard` does not. Choose by whether a miss means a bug.

  2. What does `s.discard(99)` return when 99 is not present?

    pch.quizShowAnswer

    B — `None` — It returns None and changes nothing. Useful when the element may legitimately not be there.

  3. Which element does `s.pop()` remove?

    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.

  4. You are removing IDs supplied by a user from a set of known IDs. Which method fits?

    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.

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading