>>> d = {'x': 1, 'y': 2, 'z': 3}>>> for my_var in d:>>> print my_var, 'corresponds to', d[my_var]
x corresponds to 1y corresponds to 2z corresponds to 3
…或者更好,
d = {'x': 1, 'y': 2, 'z': 3}
for the_key, the_value in d.iteritems():print the_key, 'corresponds to', the_value
对于python3. x:
d = {'x': 1, 'y': 2, 'z': 3}
for the_key, the_value in d.items():print(the_key, 'corresponds to', the_value)
dictionary= {1:"a", 2:"b", 3:"c"}
#To iterate over the keysfor key in dictionary.keys():print(key)
#To Iterate over the valuesfor value in dictionary.values():print(value)
#To Iterate both the keys and valuesfor key, value in dictionary.items():print(key,'\t', value)