John Ashby
John Ashby

Reputation: 23

Iterating over a list of nested dictionaries with unicode keys and values

I'm new to both Python and JSON. I have a list of (what I believe are) dictionaries that are made up from multiple responses to an API. More specifically, I am iterating through a list of links to images, sending them one by one to an API that processes them, and storing the results in a list.

Below is a sample of the list of dictionaries.

[[{u'attribute': {u'age': {u'range': 7, u'value': 21},
    u'gender': {u'confidence': 70.0191, u'value': u'Male'},
    u'glass': {u'confidence': 99.7953, u'value': u'None'},
    u'race': {u'confidence': 99.7875, u'value': u'White'},
    u'smiling': {u'value': 0.373321}},
   u'face_id': u'3709f4b7c37451bece7d031165301358',
   u'position': {u'center': {u'x': 45.333333, u'y': 49.851632},
    u'eye_left': {u'x': 44.417833, u'y': 48.420178},
    u'eye_right': {u'x': 45.882167, u'y': 48.280712},
    u'height': 7.121662,
    u'mouth_left': {u'x': 44.614333, u'y': 51.60089},
    u'mouth_right': {u'x': 45.693667, u'y': 51.402077},
    u'nose': {u'x': 44.954, u'y': 49.712166},
    u'width': 4.0},
   u'tag': u''}],
 [{u'attribute': {u'age': {u'range': 7, u'value': 42},
    u'gender': {u'confidence': 97.1968, u'value': u'Male'},
    u'glass': {u'confidence': 65.9269, u'value': u'None'},
    u'race': {u'confidence': 99.7245, u'value': u'White'},
    u'smiling': {u'value': 23.752}},
   u'face_id': u'809311726519d7396632edd2ad5b3511',
   u'position': {u'center': {u'x': 28.375, u'y': 56.666667},
    u'eye_left': {u'x': 19.411575, u'y': 44.480889},
    u'eye_right': {u'x': 35.815, u'y': 41.542311},
    u'height': 63.555556,
    u'mouth_left': {u'x': 20.52055, u'y': 74.964889},
    u'mouth_right': {u'x': 35.7015, u'y': 74.159111},
    u'nose': {u'x': 26.885, u'y': 60.064444},
    u'width': 35.75},
   u'tag': u''}]

What I need to do is iterate through this list, pulling values from the 'attributes' key. For example, counting the number of gender: values: male.

My problem is accessing the nested dictionaries. No matter what i try, I get errors either related to unicode objects or list indices must be integers not str.

How can I iterate through the list of dictionaries (of dictionaries) and get the gender values?

Any help would be appreciated. I have spent hours trying to do this and am not sure if the problem is the unicode values or something else.

Upvotes: 2

Views: 1784

Answers (1)

Jonathan Adam
Jonathan Adam

Reputation: 61

What this currently looks like (based on the square brackets) is that you have a list of lists of dictionaries. At this point you would have to iterate soemthing like:

for dictList in list:
     for dict in dictList:
         *do something with dict, eg* if dict['gender']['value']=='Male':
                      maleCounter += 1

I think you may be overlooking the fact that the dictionaries are nested in lists once again, which is why it's throwing errors about list indices.

Upvotes: 1

Related Questions