Can I remove several keys with one del command?


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']


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'.


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