python for loops – why does range(len( cause TypeError: int not subscriptable in this case?

I'm working on looping through a Json response and I'm trying to figure out why in this case below I get a TypeError: 'int' not subscriptable.

list = []
for i in range(len(json_data['MRData']['RaceTable']['Races'][0]['Results'])):
        list.append(i['FastestLap']['Time']['time'])

print(list)

I got around this by just doing a try:except block but I would rather know the length of what I'm iterating over. I tried reading some of the posts on here from other folks regarding this but couldn't make sense of it. Any help appreciated!



Read more here: https://stackoverflow.com/questions/68477807/python-for-loops-why-does-rangelen-cause-typeerror-int-not-subscriptable-in

Content Attribution

This content was originally published by krisbkreeme at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: