I am calling an API and getting following data structure:
{u'query': {u'pages': {u'120714': {u'ns': 0, u'pageid': 120714, u'revisions': [{u'size': 985}], u'title': u'Daniel Nannskog'}}, u'userinfo': {u'anon': u'', u'id': 0, u'name': u'2620:0:862:101:0:0:2:4'}}}
What I want is to get the size out from this data structure, I know how to extract the data from here but the problem is at the time of extraction I don't know the key(120714) after pages, for example:
lets assign this to a variable = d
>>> d
{u'query': {u'pages': {u'120714': {u'title': u'Daniel Nannskog', u'ns': 0, u'pageid': 120714, u'revisions': [{u'size': 985}]}}, u'userinfo': {u'anon': u'', u'id': 0, u'name': u'2620:0:862:101:0:0:2:4'}}}
>>> d['query']['pages']['120714']['revisions']
[{u'size': 985}]
>>>
But how can I get to size without knowing the value of the second level key prior to extraction?
d['query']['pages'][foo]['revisions']['size'] for all pagesfoo`?revisionsholds alist, not a single value, so… you can't get the size, because there may be multiple values. Do you want all of them? The first? The longest? The total?