Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Using the remove() and discard() Methods | Mastering Python Sets
Python Data Structures
course content

Kurssisisältö

Python Data Structures

Python Data Structures

2. Mastering Python Dictionaries
3. Mastering Python Tuples
4. Mastering Python Sets

book
Using the 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
Tehtävä

Swipe to start coding

You are given the set marvel_movies. However, two movies from another studio have accidentally been added to this set.

  • Remove the movie "The Dark Knight" from the set.
  • Remove the movie "Justice League" from the set.
  • Use the discard() or remove() methods to accomplish this task.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 4. Luku 5
toggle bottom row

book
Using the 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
Tehtävä

Swipe to start coding

You are given the set marvel_movies. However, two movies from another studio have accidentally been added to this set.

  • Remove the movie "The Dark Knight" from the set.
  • Remove the movie "Justice League" from the set.
  • Use the discard() or remove() methods to accomplish this task.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 4. Luku 5
Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Pahoittelemme, että jotain meni pieleen. Mitä tapahtui?
some-alt