Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Using the pop() Method: Deleting Elements with Return Values | Mastering Python Dictionaries
Python Data Structures

Swipe to show menu

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
Task

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.

Solution

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Β 2. ChapterΒ 6

Ask AI

expand
ChatGPT

Ask anything or try one of the suggested questions to begin our chat

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
Task

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.

Solution

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Β 2. ChapterΒ 6
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