Deleting more than 1 key from dictionary


#1


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

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.


#2

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.

KeyError


#3

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.


#4

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)


#5

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