Returning dictionary from a function

Diez B. Roggisch deets at nospam.web.de
Thu May 14 18:01:25 EDT 2009


kk schrieb:
> Hi
> 
> I am working on something here and I cannot get the full dictionary
> out of a function. I am sure I am missing something here.
> 
> Anyways here is a simple code that repeats my problem. Basically I am
> just trying to get that values function to return the diky as a
> dictionary so that I can query values later.
> 
> thanks
> 
> 
> 
> 
> def values(x):
>     diky={}
>     for a in range(x):
>         a=a+100
>         diky={chr(a):a}
> 
>         print diky
>     return diky
> 
> 
> b=values(5)
> print type(b),len(b), b['f'] # gives error
> print type(b),len(b), b['h'] # does not give error

You are creating a new dictionary over and over.

dikiy[chr(a)] = a

is what you want inside the loop.

Diez



More information about the Python-list mailing list