Course Content
Python Data Structures
Python Data Structures
1. Mastering Python Lists
Creating Lists in Python: Declaring and Initializing ListsList Indexing in Python: Accessing Elements EfficientlyWorking with Nested Lists in PythonPython List Length: Measuring and Managing List SizeModifying Lists in Python: Updating and Changing ElementsUsing the append() Method: Adding Elements to ListsUsing the insert() Method: Placing Elements at Specific PositionsDeleting Elements in Python Lists: Removing Items SafelyUsing the remove() Method: Deleting Specific Elements from Lists
2. Mastering Python Dictionaries
Creating a Dictionary in Python: Storing Key-Value PairsAccessing Dictionary ValuesAccessing Dictionary KeysAdding Items to a Dictionary: Updating Key-Value PairsUsing the del Keyword: Removing Dictionary EntriesUsing the pop() Method: Deleting Elements with Return ValuesUsing the popitem() Method: Removing the Last Inserted ItemUsing the clear() Method: Emptying a Dictionary Completely
3. Mastering Python Tuples
Creating a Tuple in Python: Defining Immutable Data StructuresAccessing Elements in a Tuple: Indexing TechniqueConcatenating Tuples in Python: Merging Immutable SequencesDeleting Tuples in Python: Removing References to TuplesUpdating Tuples in PythonAdding Items to a Tuple: Alternative Approaches Using ListsCounting Elements in a Tuple: Using the count() MethodFinding Elements in a Tuple: Using the index() Method for Lookup
4. Mastering Python Sets
Creating a Set in Python: Defining Unordered CollectionsUsing the add() Method: Adding Single Elements to a SetUsing the update() Method: Merging Multiple Elements into a SetAccessing Elements in a Set: Iteration and Membership TestingUsing the remove() and discard() MethodsUsing the clear() Method: Removing All Elements from a Set
Using the popitem() Method: Removing the Last Inserted Item
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)
Task
Swipe to start coding
You are given the dictionary authors_books
.
Your goal:
- 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.
Solution
Everything was clear?
Thanks for your feedback!
Section 2. Chapter 7
Using the popitem() Method: Removing the Last Inserted Item
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)
Task
Swipe to start coding
You are given the dictionary authors_books
.
Your goal:
- 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.
Solution
Everything was clear?
Thanks for your feedback!
Section 2. Chapter 7