Skip to content
Advertisement

Data Dragon items, keyError

so ive run into a problem where i cant get acces to they key in riot json file. http://ddragon.leagueoflegends.com/cdn/11.16.1/data/en_US/item.json this is official riot link which u can find on their riot api site under Items.

if you write this:

for key, value in items['data'].items():
    print(value)

it gives back all the values If you write this:

for key value in items['data'].items():
    print(value['name'])

it gives back the name, and its like this with every other value[key] thing except for the ‘depth key’. I used double for loop and it finally returned all the depth values:

for key,value in items['data'].items():
    for k,v in value.items():
        if k == 'depth':
            print(v)

But it doesnt save me cause i want to write something like, if the ‘depth’ == 3 i want to append the item ‘name’ into a list. I thought that, maybe it is an erroe because the first items in this file doesnt have ‘depth’ key so i wrote an exception but it didnt work.

Advertisement

Answer

Just use get() method to get the value of specific key. Then you can compare it with the desired value. For your case, list comprehension with simple check could do the job:

names_list = [v.get('name') for v in items['data'].values() if v.get('depth') == 3]
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement