Q: how to scan every element of a dictionary ?
Jan Dries
jdries at mail.com
Sat Oct 28 21:18:59 EDT 2000
Hwanjo Yu wrote:
> Doesn't the dictionary data structure support an enumeration operator to
> scan every element of it ?
A dictionary has 3 functions that convert the dictionary to a list that
can be enumerated:
Given :
my_dict = { ..... }
you can write:
my_dict.items() # returns a list of tuples (key,value)
my_dict.keys() # returns a list of all the keys
my_dict.values() # returns a list of all the values
These are all lists, and you can enumerate over them using for, for
example:
for i in my_dict.items():
print "key = %s, value = %s" % i
Regards,
Jan
More information about the Python-list
mailing list