セクション 1. 章 13
single
Pythonにおける辞書エントリの削除
メニューを表示するにはスワイプしてください
Pythonでは、delキーワードを使用して辞書からキーを削除できます。これにより、特定のエントリを削除したり、辞書全体をクリアしたりすることが可能です。図書館に関連する例を使って、この仕組みを見てみましょう。
del dict[key]
123456789101112book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance", "copies": 5 } # Deleting a key value del book["copies"] print(book)
辞書全体の削除
辞書全体を削除したい場合は、delを使用できます。ただし、この操作は辞書自体を完全に削除するため、以降その辞書を参照するとエラーが発生します。
1234567891011book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance" } # Deleting the entire dictionary del book # Attempting to print the dictionary after deletion will cause an error print(book) # This line will raise a NameError
タスク
スワイプしてコーディングを開始
辞書 authors_books を引き続き使用します。
- キー
"J.K. Rowling"を持つ要素を辞書から削除してください。 - これには
delキーワードを使用します。
解答
すべて明確でしたか?
フィードバックありがとうございます!
セクション 1. 章 13
single
AIに質問する
AIに質問する
何でも質問するか、提案された質問の1つを試してチャットを始めてください