pythonreference.com

search statements, functions, methods, exceptions*

show categories

'for' with a dict
iterate over each key in a dict
myd = {'a': 1, 'b': 2}

for mykey in myd:
    print(mykey)
dict -> for each str/int key: execute block with key
Argument(s):
N/A (iterates over dict keys)
Return value(s):
with each iteration, key from the dict is assigned to the loop variable
as Python iterates over each key in the dictionary, it assigns the key to the loop variable (in the example, mykey), then executes the block. The assignment and block execution happen once for each key in the dict.