Computer Science
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
Python Dictionaries
2 Likes
Answer
{'Jo': 1, 'Ra': 2, 'Phoebe': 2}
Reason — The update() method updates the dictionary with the elements from another dictionary object or from an iterable of key/value pairs.
Here, {"Phoebe":2} is a dictionary of key named "Phoebe" which is not present in dict. Therefore, update function will add this key and its corresponding value to original dictionary dict.
Note: There is a misprint in the options provided in the book.
Answered By
1 Like
Related Questions
What will be the result of the following code ?
d1 = {"abc":5,"def":6,"ghi":7} print(d1[0])- abc
- 5
- {"abc":5}
- Error
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.
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"])
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