Skip to content Skip to sidebar Skip to footer

In Dictionary Output Is Not Getting As Expected In Python

here response in an dictionary which is having form values stored in an dictionary. My output is generatd with seperate dictionary which i doesnot need seperately. I want all the e

Solution 1:

the problem is here:

b = []
for ...
   arg = {i: resp[i]}  # creates a new dict
   b.append(arg)  # adds the dict to the list

What you're probably looking for is something like:

b = {}
for ...
    b.update(arg)

Of course, this still isn't the cleanest way to do it. After all, why create all the temporary dictionaries?

b = {}
for ...
   b[i] = resp[i]

would work, or you could probably even pull it all into a dictionary comprehension.

Solution 2:

defform_getvalue(i):
    return ('0', '1', '1', '3', '1', '3', '0', '0', '0', '0')[i]

resp = {}
for i inrange(1, 10):
    resp[i] = int(form_getvalue(i))

print'resp=%s' % (resp,)

The output of the above is:

resp={1:1,2:1,3:3,4:1,5:3,6:0,7:0,8:0,9:0}

Post a Comment for "In Dictionary Output Is Not Getting As Expected In Python"