Iterating Over Dictionary in Python
Data Structures, Python
A dictionary in Python is a collection of unordered values accessed by key rather than index. Dictionary elements have no intrinsic order.
This short article shows several methods for iterating over Python dictionaries.
The examples in this article are Python 3.
Example dictionary:
Iterate via keys
Iterate values only
Iterate over key value pairs
I often confuse this with the PHP foreach
construct - forgetting to chain the items()
method to the dictionary being iterated over.
Iterate over keys in the sorted order of the keys
Reference
comments powered by Disqus