Course Content
Python Data Structures
Python Data Structures
Remove() and Discard() Methods
In sets, you can remove elements using the remove()
method.
set_1 = {10, 20, 30, 40, 50, 60, 70} print(set_1) set_1.remove(50) set_1.remove(60) print(set_1)
Note
If the element you're trying to remove isn't in the set, you'll get a
KeyError
.
set_1 = {10, 20, 30, 40, 50, 60, 70} set_1.remove(999)
There's another way to get rid of items from a set: the discard()
method. Unlike remove()
, discard()
won't throw a KeyError
if the item you want to remove isn't there — it just leaves the set as is.
set_1 = {10, 20, 30, 40, 50, 60, 70} print(set_1) # Remove() method set_1.remove(50) set_1.remove(60) print(set_1) # Discard() method set_1.discard(10) set_1.discard(20) set_1.discard(999) print(set_1)
Swipe to show code editor
You have the following set:
After modifying the set, it should look like this:
Achieve this using the remove()
method.
Thanks for your feedback!
Remove() and Discard() Methods
In sets, you can remove elements using the remove()
method.
set_1 = {10, 20, 30, 40, 50, 60, 70} print(set_1) set_1.remove(50) set_1.remove(60) print(set_1)
Note
If the element you're trying to remove isn't in the set, you'll get a
KeyError
.
set_1 = {10, 20, 30, 40, 50, 60, 70} set_1.remove(999)
There's another way to get rid of items from a set: the discard()
method. Unlike remove()
, discard()
won't throw a KeyError
if the item you want to remove isn't there — it just leaves the set as is.
set_1 = {10, 20, 30, 40, 50, 60, 70} print(set_1) # Remove() method set_1.remove(50) set_1.remove(60) print(set_1) # Discard() method set_1.discard(10) set_1.discard(20) set_1.discard(999) print(set_1)
Swipe to show code editor
You have the following set:
After modifying the set, it should look like this:
Achieve this using the remove()
method.
Thanks for your feedback!
Remove() and Discard() Methods
In sets, you can remove elements using the remove()
method.
set_1 = {10, 20, 30, 40, 50, 60, 70} print(set_1) set_1.remove(50) set_1.remove(60) print(set_1)
Note
If the element you're trying to remove isn't in the set, you'll get a
KeyError
.
set_1 = {10, 20, 30, 40, 50, 60, 70} set_1.remove(999)
There's another way to get rid of items from a set: the discard()
method. Unlike remove()
, discard()
won't throw a KeyError
if the item you want to remove isn't there — it just leaves the set as is.
set_1 = {10, 20, 30, 40, 50, 60, 70} print(set_1) # Remove() method set_1.remove(50) set_1.remove(60) print(set_1) # Discard() method set_1.discard(10) set_1.discard(20) set_1.discard(999) print(set_1)
Swipe to show code editor
You have the following set:
After modifying the set, it should look like this:
Achieve this using the remove()
method.
Thanks for your feedback!
In sets, you can remove elements using the remove()
method.
set_1 = {10, 20, 30, 40, 50, 60, 70} print(set_1) set_1.remove(50) set_1.remove(60) print(set_1)
Note
If the element you're trying to remove isn't in the set, you'll get a
KeyError
.
set_1 = {10, 20, 30, 40, 50, 60, 70} set_1.remove(999)
There's another way to get rid of items from a set: the discard()
method. Unlike remove()
, discard()
won't throw a KeyError
if the item you want to remove isn't there — it just leaves the set as is.
set_1 = {10, 20, 30, 40, 50, 60, 70} print(set_1) # Remove() method set_1.remove(50) set_1.remove(60) print(set_1) # Discard() method set_1.discard(10) set_1.discard(20) set_1.discard(999) print(set_1)
Swipe to show code editor
You have the following set:
After modifying the set, it should look like this:
Achieve this using the remove()
method.