User Tools

Site Tools


it:how-to_iterate_in_python

How-To Iterate in Python

Iterate a Dictionary

Iterate keys only:

d = {'x': 1, 'y': 2, 'z': 3} 
for key in d:
    print(key, 'corresponds to', d[key])

Iterate keys and values:

d = {'x': 1, 'y': 2, 'z': 3} 
for key, value in d.items():
    print(key, 'corresponds to', value)

Discussion

Enter your comment. Wiki syntax is allowed:
E I C D G
 
it/how-to_iterate_in_python.txt · Last modified: 2018/12/01 19:38 by pmay