Parsing Python dictionary with multiple objects

Rustom Mody rustompmody at gmail.com
Wed Oct 15 06:50:28 CEST 2014


On Wednesday, October 15, 2014 9:10:54 AM UTC+5:30, Anurag Patibandla wrote:
> Thanks for the response.
> Here is the code that I have tried.

> from operator import itemgetter
> keys = json.keys()
> order = list(keys)
> q1 = int(round(len(keys)*0.2))
> q2 = int(round(len(keys)*0.3))
> q3 = int(round(len(keys)*0.5))
> b = [q1,q2,q3]
> n=0
> for i in b:
>     queues = order[n:n+i]

>     n = n+i
>     print queues

>     for j in range(len(queues)):
>         q = (queues[j], json.get(queues[j]))
>         print q

Converting the end for loop (last 3 lines) into:


    print [(queues[j], json.get(queues[j])) for j in range(len(queues))]

Does that help?

General advice:
1. Instead of writing 'naked' code as you have done, if you wrap it into
functions (preferably small)
2. Contents similar to the original naked code but with print's replaced by 
return's

you make your as well as those trying to help/collaborate with you
life easier

Also the above is a more or mechanical translation. However

something[j] ... for j in range(len(something))

is usually a sign of a C programmer writing python :-)
Usually better to write

x for x in something

So...
Better to write that comprehension as

print [(q, json.get(q)) for q in queues]



More information about the Python-list mailing list