lopping over Dictionaries
Skip Montanaro
skip at mojam.com
Fri Sep 17 14:43:00 EDT 1999
>>>>> "Kevin" == Kevin Howe <khowe at performance-net.com> writes:
Kevin> Is it possible to iterate through list of keys in a Dictionary?
Yup. You were very close. You can do it a couple ways:
tel = {'jack': 4098, 'sape': 4139}
for name in tel.keys():
value = tel[name]
or
tel = {'jack': 4098, 'sape': 4139}
for name, value in tel.items():
value = tel[name]
If you don't really care what key and values are associated together, you
can iterate over just the values as well:
tel = {'jack': 4098, 'sape': 4139}
for value in tel.values():
pass
More information about the Python-list
mailing list