Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Using the pop() Method: Deleting Elements with Return Values | Mastering Python Dictionaries
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 pop() Method: Deleting Elements with Return Values

The pop() method in Python dictionaries allows you to remove a key-value pair based on its key and returns the corresponding value. This method is particularly useful when you need to extract and process a value while simultaneously removing it from the dictionary.

The syntax is:

python
123456789101112
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "copies": 5 } # Remove the 'copies' key and retrieve its value removed_copies = book.pop("copies") print("Updated dictionary:", book) print("Removed value:", removed_copies)
copy

If the key does not exist in the dictionary, Python raises a KeyError exception.

12345678
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813 } # Attempting to remove a non-existent key removed_genre = book.pop("genre")
copy
Tehtävä

Swipe to start coding

You are given the same dictionary authors_books.

  • Remove "Stephen King"'s books from the dictionary and save them in the variable kings_books.
  • Use the pop() method to accomplish this.

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 2. Luku 6
toggle bottom row

book
Using the pop() Method: Deleting Elements with Return Values

The pop() method in Python dictionaries allows you to remove a key-value pair based on its key and returns the corresponding value. This method is particularly useful when you need to extract and process a value while simultaneously removing it from the dictionary.

The syntax is:

python
123456789101112
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "copies": 5 } # Remove the 'copies' key and retrieve its value removed_copies = book.pop("copies") print("Updated dictionary:", book) print("Removed value:", removed_copies)
copy

If the key does not exist in the dictionary, Python raises a KeyError exception.

12345678
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813 } # Attempting to remove a non-existent key removed_genre = book.pop("genre")
copy
Tehtävä

Swipe to start coding

You are given the same dictionary authors_books.

  • Remove "Stephen King"'s books from the dictionary and save them in the variable kings_books.
  • Use the pop() method to accomplish this.

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 2. Luku 6
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