Returning dictionary from a function

K

kk

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
 
K

kk

Btw my main problem is that when I assign the function to 'b' variable
I only get the last key from the dictionary. Sorry about that I forgot
to mention the main issue.
 
J

Jeff McNeil

Btw my main problem is that when I assign the function to 'b' variable
I only get the last key from the dictionary. Sorry about that I forgot
to mention the main issue.


You're creating a new dictionary with each iteration of your loop, use
d[k] = v syntax instead.
 
D

Diez B. Roggisch

kk said:
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
 
S

smarden1

def values(x):
diky={}
for a in range(x):
a=a+100
diky[chr(a)] = a
return diky

it is not working b/c you are creating a new dictionary with each
iteration of the loop, rather you want to update the same dictionary
with the new value you have..
 
B

bearophileHUGS

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
 
M

MRAB

kk said:
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
 
K

kk

Hi


Thank you so much. It makes perfect sense. I actually tried the second
suggested syntax before posting here but it was inside of my actual
code which probably had another problem. The suggested solution works
perfectly.

thanks again
 
J

John Machin

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

And you have a SECOND problem ... rebinding your loop counter is
likely to confuse anybody (including yourself!) who tries to
understand (let alone maintain!) your code. If you want a to have
values 0, 100, 200, 300, 400 as the loop is traversed, you should use

for a in range(0, 500, 100):

or maybe you want 100, ...., 104 which would be produced by
EITHER
for a in range(100, 105):
OR
for c in range(5):
a = c + 100
        diky={chr(a):a}

Note that chr(a) will cause an exception when a >= 256 ... do you need
unichr()?

AND what is the point of this dicky diky anyway? Have you considered
the ord() function?
        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

As expected; the last value of a was 104 and ord('h') == 104 ...

HTH,
John
 
K

kk

John,

Thanks for pointing out the loop issue. I just typed these sloppy
lines the demonstrate the issue, they were not part of any code by any
means. I will make sure that I will post cleaner lines next time.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top