Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Remove() and Discard() Methods | Set
Python Data Structures
course content

Course Content

Python Data Structures

Python Data Structures

1. List
2. Dictionary
3. Tuple
4. Set

bookRemove() and Discard() Methods

In sets, you can remove elements using the remove() method.

123456
set_1 = {10, 20, 30, 40, 50, 60, 70} print(set_1) set_1.remove(50) set_1.remove(60) print(set_1)
copy

Note

If the element you're trying to remove isn't in the set, you'll get a KeyError.

12
set_1 = {10, 20, 30, 40, 50, 60, 70} set_1.remove(999)
copy

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.

12345678910111213
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)
copy
Task
test

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.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 5
toggle bottom row

bookRemove() and Discard() Methods

In sets, you can remove elements using the remove() method.

123456
set_1 = {10, 20, 30, 40, 50, 60, 70} print(set_1) set_1.remove(50) set_1.remove(60) print(set_1)
copy

Note

If the element you're trying to remove isn't in the set, you'll get a KeyError.

12
set_1 = {10, 20, 30, 40, 50, 60, 70} set_1.remove(999)
copy

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.

12345678910111213
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)
copy
Task
test

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.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 5
toggle bottom row

bookRemove() and Discard() Methods

In sets, you can remove elements using the remove() method.

123456
set_1 = {10, 20, 30, 40, 50, 60, 70} print(set_1) set_1.remove(50) set_1.remove(60) print(set_1)
copy

Note

If the element you're trying to remove isn't in the set, you'll get a KeyError.

12
set_1 = {10, 20, 30, 40, 50, 60, 70} set_1.remove(999)
copy

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.

12345678910111213
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)
copy
Task
test

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.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

In sets, you can remove elements using the remove() method.

123456
set_1 = {10, 20, 30, 40, 50, 60, 70} print(set_1) set_1.remove(50) set_1.remove(60) print(set_1)
copy

Note

If the element you're trying to remove isn't in the set, you'll get a KeyError.

12
set_1 = {10, 20, 30, 40, 50, 60, 70} set_1.remove(999)
copy

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.

12345678910111213
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)
copy
Task
test

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.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 4. Chapter 5
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt