Practice mode Python

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].

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.