KnowledgeBoat Logo
|

Computer Science

What will be the result of the following code?

d = {"Jo":1,"Ra":2} 
d.update({"Phoebe":2})
print(dict)
  1. {"Jo":1,"Ra":2,"Ph":2}
  2. {"Jo":1,"Ra":2}
  3. {"Jo":1,"Ph":2}
  4. 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