Can I remove several keys with one del command?


#1

https://www.codecademy.com/en/courses/python-beginner-en-pwmb1/2/3?curriculum_id=4f89dab3d788890003000096#

zoo_animals = { 'Unicorn' : 'Cotton Candy House',
'Sloth' : 'Rainforest Exhibit',
'Bengal Tiger' : 'Jungle House',
'Atlantic Puffin' : 'Arctic Exhibit',
'Rockhopper Penguin' : 'Arctic Exhibit'}

del zoo_anomals['Bengal Tiger', 'Penguin']

#2

Hi @sunsleder ,

You have a spelling error here ...

del zoo_anomals['Bengal Tiger', 'Penguin']

It should be zoo_animals.

If you do this ...

del zoo_animals['Bengal Tiger', 'Penguin']

... the Python interpreter looks for the single key, ('Bengal Tiger', 'Penguin'), which is a tuple, and which does not exist as a key in the dictionary that you have created.

But, you can do this to remove multiple items ...

del zoo_animals['Bengal Tiger'], zoo_animals['Rockhopper Penguin']

Note that you do not have a 'Penguin' in your dictionary, but you do have a 'Rockhopper Penguin'.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.