Iterating Dictionaries in a Function
Thinker
thinkengine1 at yahoo.com
Fri May 7 21:23:10 EDT 2004
I could not get this simple code work. I am guessing that there is a
bug
in the Python dictionary iterator.
def findx(st):
d = {'a': 1, 'rty': 4, 'er': 2}
for item in d.keys():
print item
if cmp(item.upper(),st.upper()) == 0:
print d[item]
return d[item]
else:
return st
When I call:
findx('a')
it finds the key. But when I call:
findx('er')
it does not find anything since it does not iterate; it just checks
the first item in the dictionary.
Does anyone see where the bug is?
More information about the Python-list
mailing list