Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Utilisation du Mot-clé del : Suppression des Entrées de Dictionnaire | Dictionnaire
Structures de Données Python

book
Utilisation du Mot-clé del : Suppression des Entrées de Dictionnaire

En Python, vous pouvez supprimer des clés d'un dictionnaire en utilisant le mot-clé del. Cela vous permet de supprimer des entrées spécifiques ou même de vider entièrement le dictionnaire. Explorons comment cela fonctionne à l'aide d'un exemple lié à une bibliothèque.

python
del dict[key]
book = {
"title": "Pride and Prejudice",
"author": "Jane Austen",
"year": 1813,
"genre": "Romance",
"copies": 5
}

# Deleting a key value
del book["copies"]

print(book)
123456789101112
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance", "copies": 5 } # Deleting a key value del book["copies"] print(book)
copy

Suppression de l'ensemble du dictionnaire

Si vous souhaitez supprimer l'ensemble du dictionnaire, vous pouvez le faire avec del. Cependant, soyez prudent—cette action supprime complètement le dictionnaire, et toute référence ultérieure à celui-ci entraînera une erreur.

book = {
"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
1234567891011
book = { "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
copy
Tâche

Swipe to start coding

Nous continuons à travailler avec le dictionnaire authors_books.

  • Supprimez l'élément avec la clé "J.K. Rowling" du dictionnaire.
  • Utilisez le mot-clé del pour cela.

Solution

authors_books = {
'William Shakespeare': ['Hamlet', 'Macbeth', 'Romeo and Juliet', 'Othello'],
'J.K. Rowling': ['Harry Potter and the Sorcerer\'s Stone', 'Harry Potter and the Chamber of Secrets', 'Harry Potter and the Prisoner of Azkaban', 'Harry Potter and the Goblet of Fire'],
'George Orwell': ['1984', 'Animal Farm', 'Coming Up for Air'],
'Stephen King': ['It', 'The Shining', 'Carrie', 'Misery'],
'Agatha Christie': ['Murder on the Orient Express', 'The Murder of Roger Ackroyd', 'And Then There Were None', 'Death on the Nile']
}

# Write your code here
del authors_books['J.K. Rowling']

# Testing
print("Updated dictionary:", authors_books)
Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 5
authors_books = {
'William Shakespeare': ['Hamlet', 'Macbeth', 'Romeo and Juliet', 'Othello'],
'J.K. Rowling': ['Harry Potter and the Sorcerer\'s Stone', 'Harry Potter and the Chamber of Secrets', 'Harry Potter and the Prisoner of Azkaban', 'Harry Potter and the Goblet of Fire'],
'George Orwell': ['1984', 'Animal Farm', 'Coming Up for Air'],
'Stephen King': ['It', 'The Shining', 'Carrie', 'Misery'],
'Agatha Christie': ['Murder on the Orient Express', 'The Murder of Roger Ackroyd', 'And Then There Were None', 'Death on the Nile']
}

# Write your code here
___

# Testing
print("Updated dictionary:", authors_books)

Demandez à l'IA

expand
ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

some-alt