Computer Science
Which of the following will delete key_value pair for key="tiger" in dictionary?
di = {"lion":"wild","tiger":"wild","cat": "domestic", "dog":"domestic"}
- del di["tiger"]
- di["tiger"].delete( )
- delete(di["tiger"])
- del(di.["tiger"])
Python Dictionaries
3 Likes
Answer
del di["tiger"]
Reason — del keyword is used to delete an item with the specified key name. Here, tiger is the key name which is specified with del statement in expression: del di["tiger"]. Hence, it will delete tiger entry from the di.
Answered By
2 Likes
Related Questions
What will the following code do?
d = {"Phy":94, "Che":70, "Bio":82, "Eng":95} d.update({"Che":72, "Bio":80})- It will create new dictionary as dict={"Che":72,"Bio":80} and old d will be deleted.
- It will throw an error as dictionary cannot he updated.
- It will simply update the dictionary as dict={"Phy":94, "Che":72, "Bio":80, "Eng":95}.
- It will not throw any error but it will not do any changes in dict.
What will be the result of the following code?
d = {"Jo":1,"Ra":2} d.update({"Phoebe":2}) print(dict)- {"Jo":1,"Ra":2,"Ph":2}
- {"Jo":1,"Ra":2}
- {"Jo":1,"Ph":2}
- Error
Which of the following will give error if d1 is as shown below?
d1 = {"a":1, "b":2, "c":3}- print(len(d1))
- print(d1.get("b"))
- d1["a"] = 5
- None of these
Which of the following Python codes will give the same output if
dict = {"diary":1, "book":3, "novel":5}(i) dict.pop("book")
(ii) del dict["book"]
(iii) dict.update({"diary":1,"novel":5})- (i), (ii), (iii)
- (1), (ii)
- (i), (iii)
- (ii), (iii)