Course Content
Python Data Structures
Python Data Structures
The popitem() Method
Dictionaries also have a popitem()
method that removes and returns the last key-value pair.
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)
Swipe to show code editor
Use the popitem()
method to remove the last added key-value pair from the book
dictionary and print it.
Note
The
popitem()
method returns the removed pair as a tuple.
Thanks for your feedback!
The popitem() Method
Dictionaries also have a popitem()
method that removes and returns the last key-value pair.
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)
Swipe to show code editor
Use the popitem()
method to remove the last added key-value pair from the book
dictionary and print it.
Note
The
popitem()
method returns the removed pair as a tuple.
Thanks for your feedback!
The popitem() Method
Dictionaries also have a popitem()
method that removes and returns the last key-value pair.
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)
Swipe to show code editor
Use the popitem()
method to remove the last added key-value pair from the book
dictionary and print it.
Note
The
popitem()
method returns the removed pair as a tuple.
Thanks for your feedback!
Dictionaries also have a popitem()
method that removes and returns the last key-value pair.
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)
Swipe to show code editor
Use the popitem()
method to remove the last added key-value pair from the book
dictionary and print it.
Note
The
popitem()
method returns the removed pair as a tuple.