What is the output of the following Python code? d = {“b”: 1, “a”: 2}; d = 3; print(list(d))
Correct answer C. ['b', 'a', 'c']
Explanation
Since Python 3.7 dictionaries are guaranteed to preserve insertion order, so iterating over d yields its keys in the order they were added: b, a, then c. Dictionaries are not sorted by key. Converting a dict to a list gives its keys, not its values, which rules out [1, 2, 3].
Answer checked How we write and check questions
Discussion