Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Deleting Elements and Storing them in Python | Section
Python Data Structures
セクション 1.  14
single

single

bookDeleting Elements and Storing them in Python

メニューを表示するにはスワイプしてください

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:

dictionary.pop(key)
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
タスク

スワイプしてコーディングを開始

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.

解答

Switch to desktop実践的な練習のためにデスクトップに切り替える下記のオプションのいずれかを利用して、現在の場所から続行する
すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 1.  14
single

single

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

some-alt