Home > Net >  How to get the object values inside the list of dictionaries?
How to get the object values inside the list of dictionaries?

Time:08-26

TEAMS = {
    'teamA': {'name': "Brazil",'points': 0,'goals_for': 0,'goals_difference': 0,'goals_against': 0,},
    'teamB': {'name': "Germany",'points': 0,'goals_for': 0,'goals_difference': 0,'goals_against': 0,},
}

MATCHES = [
    {'home': TEAMS['teamA'], 'goalsHome': 0, 'away': TEAMS['teamB'], 'goalsAway': 0},
]

def showMatch(match):
  print(f"Team {match[home][name]} X {match[away][name]} Team")


showMatch(MATCHES)

I've this list of dictionaries and the dictionary, 'home' is the 'teamA' object that have 'name', 'points'... and I wanna get the home name.

The output wanted of the print in the showMatch() function is:

Team Brazil X Germany Team

CodePudding user response:

[SOLVED]

def showMatch():
    for match in MATCHES:
        print(f"{match['home']['name']} {match['goalsHome']} X {match['goalsAway']} {match['away']['name']}\n")

showMatch()

CodePudding user response:

Use a loop.

for match in MATCHES:
    showMatch(match)

BTW, in Python that's a list of dictionaries, not an array of objects.

  • Related