Removing Items
Lesson 7 of 14 in Coddy's Dictionary in Python course.
To remove a key-value pair from a dictionary use the del statement or the pop() method,
my_dict = {"name": "Alice", "age": 30, "city": "New York"}
del my_dict["city"] # Remove the "city" key-value pair
my_dict.pop("age") # Remove the "age" key-value pairChallenge
EasyYou're given a dictionary representing a playlist of songs and their durations. Your task is to modify the playlist by updating the durations of existing songs, adding new songs, and removing specific songs. Perform the following updates:
- Update the duration of "Song1" to 200 seconds.
- Add a new song, "Song5," with a duration of 220 seconds.
- Update the duration of "Song2" to 195 seconds.
- Remove "Song3" from the playlist.
Try it yourself
# The playlist dictionary
playlist = {
"Song1": 180, # Duration in seconds
"Song2": 210,
"Song3": 160,
"Song4": 190
}
# Your code here
# Don't change below this line
print("Updated Playlist:", playlist)