wrxryuu
wrxryuu

Reputation: 33

Get parent keys in a nested dictionary

I looked through some other answers but I don't fully understand. There are no duplicate values.

{
  "type": "champion",
  "data": { 
    "89": {
      "title": "the Radiant Dawn",
      "name": "Leona"
    },
    "110":{
      "title": "the Arrow of Retribution",
      "name": "Varus"
    }
  }
}

what I have, I'm not sure how to proceed. In the actual dict there's more information than just title and key

championID = 0
for key, value in championData["data"].items():
    for childkey,childvalue in value.items():
        #
champion = getChamp(championID)

I want to input a name and have it return the ID (the number, 89 and 110 are listed). For example, inputting Leona would return 89.

(Sorry, I could have done a better job of asking the question at the beginning :'v)

Upvotes: 0

Views: 2813

Answers (1)

eli-bd
eli-bd

Reputation: 1634

This will work:

championData = {"type": "champion", "data": {
    "89": {
        "title": "the Radiant Dawn",
        "name": "Leona"
    },
    "110": {
        "title": "the Arrow of Retribution",
        "name": "Varus"
    }
}}

name = "Leona"
data = championData['data']
for championId in data:
    if(data[championId]['name']) == name:
        print(championId)

The output is: 89

Upvotes: 1

Related Questions