What does setdefault(key, default) do in Python?
Correct answer
B. Returns the value if key exists, otherwise inserts default and returns it
5 views
B. Returns the value if key exists, otherwise inserts default and returns it
5 views
setdefault inserts the key with default only when it is absent.
Asked in: Python Interview Dictionaries
https://docs.python.org/3/tutorial/datastructures.html#dictionariesNot sure why the answer is B? Ask here and get an explanation.