So far we have learned:
- How to create a dictionary
- How to add elements to a dictionary
- How to update elements in a dictionary
- How to use a dict comprehension to create a dictionary from two lists
Let’s practice these skills!
Instructions
We are building a music streaming service. We have provided two lists, representing songs in a user’s library and the amount of times each song has been played.
Using a dict comprehension, create a dictionary called plays
that goes through zip(songs, playcounts)
and creates a song
:playcount
pair for each song
in songs
and each playcount
in playcounts
.
Print plays
.
After printing plays
, add a new entry to it. The entry should be for the song "Purple Haze"
and the playcount is 1
.
This user has caught Aretha Franklin fever and listened to “Respect” 5 more times. Update the value for "Respect"
to be 94 in the plays
dictionary.
Create a dictionary called library
that has two key: value pairs:
- key
"The Best Songs"
with a value ofplays
, the dictionary you created - key
"Sunday Feelings"
with a value of an empty dictionary
Print library
.