Utilisation de la Méthode clear() : Vider Complètement un Dictionnaire
Les dictionnaires offrent également une méthode pratique clear()
qui supprime tous les éléments, vous laissant avec un dictionnaire vide. Voici comment cela fonctionne.
# Creating a dictionary for book details book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance" } # Printing the dictionary before clearing print("Before clearing:", book) # Clearing all items from the dictionary book.clear() # Printing the dictionary after clearing print("After clearing:", book)
Tâche
Swipe to start coding
Nous avons fait un excellent travail avec le dictionnaire authors_books
, mais il est temps de lui dire au revoir.
- Videz le dictionnaire
authors_books
. - Utilisez la méthode
clear()
pour cela.
Solution
Tout était clair ?
Merci pour vos commentaires !
Section 2. Chapitre 8