Background:
I was tring to write a for loop with a dictionary object and realizing that if directly use the 'key' and 'value' as indexs in the loop, it will return the following error message:
ValueError: too many values to unpack (expected 2)
Causes:
This is because each item in a dictionary is a value. Keys and values are not two separate values in the dictionary.
That is, {key:value} is considerd as one single value not a pair of two values, thus, when setting key and value both as indexs in a loop, it will not match.
Solution:
To solve the problem, we need to unpack each values in the dictionary.
Applying dict.items() method to make the loop work: The method items() returns a list of dict's (key, value) tuple pairs