Changing your mind


key - animal_name : value - location

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

A dictionary (or list) declaration may break across multiple lines

Removing the ‘Unicorn’ entry. (Unicorns are incredibly expensive.)

del zoo_animals[‘Unicorn’]

del zoo_animals[‘Sloth’]
del zoo_animals[‘Bengal Tiger’]

zoo_animals[‘Rockhopper Penguin’] = [‘Arctic Exhibit’]

print zoo_animals


I had a quick question. With regards to DELETING i tried
del zoo_animals[‘Sloth’, and ‘Bengal Tiger’]
but it did not work is there a way to just place one entry instead of having to place 2 separate ones?


Sure, you can do this:

del zoo_animals['Unicorn'], zoo_animals['Sloth']

Give it a try.


Using a google search
python using del on more then one dictionary key
will give you a whole range of articles with discussions / opinions


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