Returning dictionary from a function
bearophileHUGS at lycos.com
bearophileHUGS at lycos.com
Thu May 14 18:04:20 EDT 2009
kk:
>I am sure I am missing something here.<
This instruction created a new dicky dict for every iteration:
diky={chr(a):a}
What you want is to add items to the same dict, that you later output.
The normal way to do it is:
diky[chr(a)] = a
Your fixed code:
def values(x):
diky = {}
for i in xrange(x):
i += 100
diky[chr(i)] = i
print diky
return diky
b = values(5)
print type(b), len(b), b['f']
print type(b), len(b), b['h']
Bye,
bearophile
More information about the Python-list
mailing list