Aero Aero - 1 year ago 76
Python Question

Python for loop iterating 'i'

I am using the following script:

tagRequest = requests.get("" + tag + "/media/recent?client_id=" + clientId)
tagData = json.loads(tagRequest.text)
tagId = tagData["data"][0]["user"]["id"]

for i in tagData["data"]:
print tagData["data"][i]

My script is supposed to iterate over the JSON object, tagData. (Over everything in "data".) However, I am getting the following error:
list indices must be integers, not dict.

Answer Source

You are iterating over the contents of tagData['data'] not its indices, so:

for i in tagData["data"]:
    print i

Or indices:

for i in xrange(len(tagData["data"])):
    print tagData["data"][i]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download