Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
The 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

bookThe remove() and discard() Methods

Sets in Python allow you to remove elements using the remove() and discard() methods. While both methods remove specific elements, there is a key difference:

  • remove(): raises a KeyError if the element is not in the set;
  • discard(): does not raise an error if the element is not found; it simply leaves the set unchanged.

Let's explore these methods using a movie collection theme.

123456
# Attempting to remove movies movies = {"Inception", "Interstellar", "Tenet", "Dunkirk"} # Remove specific movies movies.remove("Dunkirk") movies.remove("Avatar") # This will raise a `KeyError`
copy

Now, let's use the discard() method, which behaves similarly but avoids errors if the specified movie is not in the set.

12345678910
# Define a set of favorite movies movies = {"Inception", "Interstellar", "Tenet", "Dunkirk", "Memento"} # Remove specific movies using discard movies.discard("Dunkirk") movies.discard("Memento") movies.discard("Avatar") # Print the result print("Final set:", movies)
copy
Task
test

Swipe to show code editor

You decide to remove "Dunkirk" from your favorite_movies set. Use the remove() method for this task. Then, try to remove "Avatar" using the discard() method to ensure no errors occur. Print the updated set.

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

bookThe remove() and discard() Methods

Sets in Python allow you to remove elements using the remove() and discard() methods. While both methods remove specific elements, there is a key difference:

  • remove(): raises a KeyError if the element is not in the set;
  • discard(): does not raise an error if the element is not found; it simply leaves the set unchanged.

Let's explore these methods using a movie collection theme.

123456
# Attempting to remove movies movies = {"Inception", "Interstellar", "Tenet", "Dunkirk"} # Remove specific movies movies.remove("Dunkirk") movies.remove("Avatar") # This will raise a `KeyError`
copy

Now, let's use the discard() method, which behaves similarly but avoids errors if the specified movie is not in the set.

12345678910
# Define a set of favorite movies movies = {"Inception", "Interstellar", "Tenet", "Dunkirk", "Memento"} # Remove specific movies using discard movies.discard("Dunkirk") movies.discard("Memento") movies.discard("Avatar") # Print the result print("Final set:", movies)
copy
Task
test

Swipe to show code editor

You decide to remove "Dunkirk" from your favorite_movies set. Use the remove() method for this task. Then, try to remove "Avatar" using the discard() method to ensure no errors occur. Print the updated set.

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

bookThe remove() and discard() Methods

Sets in Python allow you to remove elements using the remove() and discard() methods. While both methods remove specific elements, there is a key difference:

  • remove(): raises a KeyError if the element is not in the set;
  • discard(): does not raise an error if the element is not found; it simply leaves the set unchanged.

Let's explore these methods using a movie collection theme.

123456
# Attempting to remove movies movies = {"Inception", "Interstellar", "Tenet", "Dunkirk"} # Remove specific movies movies.remove("Dunkirk") movies.remove("Avatar") # This will raise a `KeyError`
copy

Now, let's use the discard() method, which behaves similarly but avoids errors if the specified movie is not in the set.

12345678910
# Define a set of favorite movies movies = {"Inception", "Interstellar", "Tenet", "Dunkirk", "Memento"} # Remove specific movies using discard movies.discard("Dunkirk") movies.discard("Memento") movies.discard("Avatar") # Print the result print("Final set:", movies)
copy
Task
test

Swipe to show code editor

You decide to remove "Dunkirk" from your favorite_movies set. Use the remove() method for this task. Then, try to remove "Avatar" using the discard() method to ensure no errors occur. Print the updated set.

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!

Sets in Python allow you to remove elements using the remove() and discard() methods. While both methods remove specific elements, there is a key difference:

  • remove(): raises a KeyError if the element is not in the set;
  • discard(): does not raise an error if the element is not found; it simply leaves the set unchanged.

Let's explore these methods using a movie collection theme.

123456
# Attempting to remove movies movies = {"Inception", "Interstellar", "Tenet", "Dunkirk"} # Remove specific movies movies.remove("Dunkirk") movies.remove("Avatar") # This will raise a `KeyError`
copy

Now, let's use the discard() method, which behaves similarly but avoids errors if the specified movie is not in the set.

12345678910
# Define a set of favorite movies movies = {"Inception", "Interstellar", "Tenet", "Dunkirk", "Memento"} # Remove specific movies using discard movies.discard("Dunkirk") movies.discard("Memento") movies.discard("Avatar") # Print the result print("Final set:", movies)
copy
Task
test

Swipe to show code editor

You decide to remove "Dunkirk" from your favorite_movies set. Use the remove() method for this task. Then, try to remove "Avatar" using the discard() method to ensure no errors occur. Print the updated set.

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