Skip to content Skip to sidebar Skip to footer

Iterate Over Nested Dictionary With Similar Keys But Different Values

{ 'matchesPlayed':{'label':'Matches Played','value':7} , 'matches':[ { 'date':'21 Jun' ,'hteamcode':'ESP' ,'hteamName':'Spain' ,'ateam

Solution 1:

Try this simpler version:

withopen(r'C:/Json/Spain.json') as f:
    data = json.load(f)

for match in data['matches']:
    print(match['hteamName'])

It should get you started with the rest of your work.

Solution 2:

["hteamName :"+ x.get("hteamName") for x in d["matches"] ]
['hteamName :Spain', 'hteamName :Chile', 'hteamName :Germany', 'hteamName :Spain',    'hteamName :Netherlands', 'hteamName :Paraguay', 'hteamName :Spain']

In a function:

def search(d, k,nested):
    result=[]
    for n in d[nested]:
        result.append('{0} : {1}'.format(k,n.get(k)))
    returnresultsearch(d,"hteamName","matches")
['hteamName : Spain', 'hteamName : Chile', 'hteamName : Germany', 'hteamName : Spain', 'hteamName : Netherlands', 'hteamName : Paraguay', 'hteamName : Spain']

This should get you started.

Post a Comment for "Iterate Over Nested Dictionary With Similar Keys But Different Values"