Dictionaries & Sets · Question 7 · Easy

From Python 3.7 onwards, in what order does a for loop go through the keys of a dictionary?

  1. In the order in which the keys were inserted
  2. In ascending sorted order of the keys
  3. In an arbitrary order that may differ from run to run
  4. In ascending order of the values
Answer and explanation

Answer: A. In the order in which the keys were inserted

Explanation: Since Python 3.7 a dictionary is guaranteed to remember insertion order, and iteration follows it. Keys are not sorted automatically; sorted(d) is needed for that.

Previous: The union and the intersection of two sets are measured. What does this print? · Next: A key is added and an existing key is updated before the keys are listed. What…

Practise dictionaries & sets

Try a fresh set of dictionaries & sets questions with an explanation after every answer, free and without signing up: Dictionaries & Sets practice. Or sit a full placement mock test.

More dictionaries & sets questions

More python practice

Conditions & Loops · Functions & Scope · Comprehensions & Generators · Classes & Objects · Exceptions & Files · Built-ins & Standard Library · Variables, Types & Operators · Python Strings

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge