Returning dictionary from a function

MRAB google at mrabarnett.plus.com
Thu May 14 18:08:55 EDT 2009


kk wrote:
> 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}

Here you're discarding the current dict and assigning another to diky.
It think you mean:

         diky[chr(a)] = a

> 
>         print diky

Here you're printing out the entire dict.

>     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




More information about the Python-list mailing list