Deleting more than 1 key from dictionary


Traceback (most recent call last):
File "python", line 13, in
KeyError: ('Sloth', 'Bengal Tiger')

why do we need to write:
del zoo_animals['Sloth']
del zoo_animals['Bengal Tiger']??

Cant you put more than 1 key in the bracket at the same time? For example

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

Replace this line with your code.


One key corresponds to one value of dictionary.
Another thing to note that keys are unique in dictionary.
So doing that is invalid and against syntax rule of python.



That makes sense, but these are 2 different names of keys. My question is moreso why we can't delete 2 at the same time. Why we would need to do 2 separate delete lines for 2 keys.


Okay first thing to note that we can't do that using del statement.

Why ?

Because we can access one value corresponding to one key at a time. (learned from previous exercise).

Passing more than one key to get corresponding values of dictionary? It is not valid in python.

(We could have deleted more than one items at a time but Dictionary does not have slicing)


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