You can iterate over a key, a pair of dictionary values as follows: -
for k, v in dic.items(): print(k, v)
what the code above does is first convert the dictionary to a list of tuples, which are then unpacked one by one, iterating into k, v as well: -
k, v = (k, v)
Now in your case, consider this code: -
z = [x.items() for x in lst] print(z)
Exit
[dict_items([('key1', 1)]), dict_items([('key2', 2)]), dict_items([('key3', 3)])]
ie list of tuple lists.
So, we rewrite your code as: -
for k, v in z: print(k, v)
Where z is a list of tuple lists. At each iteration, it selects a list (which contains only one item) from the parent list, and then tries: -
k, v = [(key, value)]
Hope this was helpful.
gautamaggarwal
source share