Aero Aero - 21 days ago 6
Python Question

Python for loop iterating 'i'

I am using the following script:

tagRequest = requests.get("https://api.instagram.com/v1/tags/" + 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

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]
Comments