Search code examples
pythontypeerrorriot-games-api

TypeError: list indices must be inegers or slices, not str (Riot Games Python API)


I have tried many potential solutions offered on the site and across google but none have given me any success. I'm trying to pull a specific item from a list following a request to Riot Games API.

My code is:

lol_watcher = LoLWatcher('<API_KEY>')

my_region = 'na1'

me = lol_watcher.summoner.by_name(my_region, 'doublelift')

my_ranked_stats = lol_watcher.league.by_summoner(my_region, me['id'])

print(my_ranked_stats)

This outputs the following:

[{'leaguId': <leagueID>, 'queueType': <queueType>, 'tier': <tier>,...}]

My issue comes in when I try and print only 'tier': print(my_ranked_stats['tier'])

If I build that way, it throws TypeError: list indices must be integers or slices, not str.

Any help with this is appreciated.


Solution

  • That is because the my_ranked_stats is a list. The 'tier' key exists in the dictionary that is the first first element of the my_ranked_stats list. So you have to access ['tier'] from the zeroth index of the list like this:

    print(my_ranked_stats[0]['tier'])