セクション 1. 章 13
single
Removing Dictionary Entries in Python
メニューを表示するにはスワイプしてください
In Python, you can remove keys from a dictionary using the del keyword. This allows you to delete specific entries or even clear the entire dictionary. Let's explore how this works using a library-related example.
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)
Deleting the Entire Dictionary
If you want to delete the entire dictionary, you can do so with del. However, be careful—this action removes the dictionary completely, and any further reference to it will raise an error.
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
タスク
スワイプしてコーディングを開始
We are continuing to work with the dictionary authors_books.
- Remove the element with the key
"J.K. Rowling"from the dictionary. - Use the
delkeyword to do this.
解答
すべて明確でしたか?
フィードバックありがとうございます!
セクション 1. 章 13
single
AIに質問する
AIに質問する
何でも質問するか、提案された質問の1つを試してチャットを始めてください