Chapter 10 Dictionaries

10.4 Dicts and LoopsΒΆ

Dicts are iterable. The for loop can be used to traverse a dict object, iterating through all the keys in the dict.

In the example above, the dict d is used as the container object in the for line, in the same way that tuples and lists are used in for lines. When a dict object is traversed, its keys are enumerated.

During the iteration over a dict object, the dict object cannot be modified. Such a modification can lead to a runtime error being raised.

© Copyright 2024 GS Ng.

Next Section - 10.5 Dicts and Functions