Menu
Coddy logo textTech

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 pair
challenge icon

Challenge

Easy

You'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:

  1. Update the duration of "Song1" to 200 seconds.
  2. Add a new song, "Song5," with a duration of 220 seconds.
  3. Update the duration of "Song2" to 195 seconds.
  4. 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)

All lessons in Dictionary in Python