Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Using the popitem() Method: Removing the Last Inserted Item | Mastering Python Dictionaries
Python Data Structures
course content

Contenuti del Corso

Python Data Structures

Python Data Structures

2. Mastering Python Dictionaries
3. Mastering Python Tuples
4. Mastering Python Sets

book
Using the popitem() Method: Removing the Last Inserted Item

Dictionaries also have a popitem() method that removes and returns the last key-value pair.

12345678910111213
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance" } # Removing the last key-value pair using popitem() popped_item = book.popitem() # Printing the updated dictionary and the removed pair print("Removed item:", popped_item) print("Updated dictionary:", book)
copy
Compito

Swipe to start coding

You are given the dictionary authors_books.

  • Remove the last element from the dictionary.
  • Add a new element with the key 'Mark Twain' and the value ['The Adventures of Tom Sawyer', 'Adventures of Huckleberry Finn', 'The Prince and the Pauper'].
  • Use the popitem() method to remove the last element.

Soluzione

Switch to desktopCambia al desktop per esercitarti nel mondo realeContinua da dove ti trovi utilizzando una delle opzioni seguenti
Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 7
toggle bottom row

book
Using the popitem() Method: Removing the Last Inserted Item

Dictionaries also have a popitem() method that removes and returns the last key-value pair.

12345678910111213
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance" } # Removing the last key-value pair using popitem() popped_item = book.popitem() # Printing the updated dictionary and the removed pair print("Removed item:", popped_item) print("Updated dictionary:", book)
copy
Compito

Swipe to start coding

You are given the dictionary authors_books.

  • Remove the last element from the dictionary.
  • Add a new element with the key 'Mark Twain' and the value ['The Adventures of Tom Sawyer', 'Adventures of Huckleberry Finn', 'The Prince and the Pauper'].
  • Use the popitem() method to remove the last element.

Soluzione

Switch to desktopCambia al desktop per esercitarti nel mondo realeContinua da dove ti trovi utilizzando una delle opzioni seguenti
Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 7
Switch to desktopCambia al desktop per esercitarti nel mondo realeContinua da dove ti trovi utilizzando una delle opzioni seguenti
Siamo spiacenti che qualcosa sia andato storto. Cosa è successo?
some-alt