Dictionary Functions Workout
- 01:55
Apply dictionary functions in Python.
Downloads
Glossary
dictionary PythonTranscript
First, create a new item with the key of the string, MRK, and the value of a list containing current price 69.98 and target price 74.53. And then once you create that item, print the entire value, that entire list of the key MRK, then remove JNJ, Johnson and Johnson and print the dictionary keys. You should then see that Merck is included and Johnson and Johnson has been removed.
We're gonna add a new item for Merck and then we're going to remove the item for Johnson and Johnson.
We add Merck by typing the name of our dictionary and then just writing a new key that doesn't exist yet in the dictionary. So in this case, we're gonna use MRK for Merck, and we're gonna set that equal to a list with the values given in the exercise. And with that line, we've created the new item with the key of a string, MRK, and then the value of this list containing the current price and the target price.
Now we're going to remove Johnson and Johnson with the delete the DEL function.
So I'm gonna write DEL price dict and then the key of the item I want to remove, which in this case is Johnson and Johnson. And now finally, I'm just gonna print price dict keys, and that's going to confirm that we've done this correctly. Here you can see that Merck has been added to our dictionary and we've removed Johnson and Johnson.