I'm traversing a json response in which stats are grouped by games played. I want to gather all of the values from the set and assign a single dictionary key to them. Currently every value has its own key. Here is a sample of the json response:
{'data': [{'ast': 9,
'blk': 0,
'dreb': 4,
'fg3_pct': 0.0,
'fg3a': 2,
'fg3m': 0,
'fg_pct': 0.6,
'fga': 20,
'fgm': 12,
'ft_pct': 0.333,
'fta': 3,
'ftm': 1,
'game': {'date': '2003-10-29T00:00:00.000Z',
'home_team_id': 26,
'home_team_score': 106,
'id': 15946,
'period': 4,
'postseason': False,
'season': 2003,
'status': 'Final',
'time': ' ',
'visitor_team_id': 6,
'visitor_team_score': 92},
'id': 361330,
'min': '42:50',
'oreb': 2,
'pf': 3,
'player': {'first_name': 'LeBron',
'height_feet': 6,
'height_inches': 8,
'id': 237,
'last_name': 'James',
'position': 'F',
'team_id': 14,
'weight_pounds': 250},
'pts': 25,
'reb': 6,
'stl': 4,
'team': {'abbreviation': 'CLE',
'city': 'Cleveland',
'conference': 'East',
'division': 'Central',
'full_name': 'Cleveland Cavaliers',
'id': 6,
'name': 'Cavaliers'},
'turnover': 2},
{'ast': 8,
'blk': 0,
'dreb': 10,
'fg3_pct': 0.2,
'fg3a': 5,
'fg3m': 1,
'fg_pct': 0.471,
'fga': 17,
'fgm': 8,
'ft_pct': 0.571,
'fta': 7,
'ftm': 4,
'game': {'date': '2003-10-30T00:00:00.000Z',
'home_team_id': 24,
'home_team_score': 95,
'id': 16277,
'period': 4,
'postseason': False,
'season': 2003,
'status': 'Final',
'time': ' ',
'visitor_team_id': 6,
'visitor_team_score': 86},
'id': 361523,
'min': '40:21',
'oreb': 2,
'pf': 1,
'player': {'first_name': 'LeBron',
'height_feet': 6,
'height_inches': 8,
'id': 237,
'last_name': 'James',
'position': 'F',
'team_id': 14,
'weight_pounds': 250},
'pts': 21,
'reb': 12,
'stl': 1,
'team': {'abbreviation': 'CLE',
'city': 'Cleveland',
'conference': 'East',
'division': 'Central',
'full_name': 'Cleveland Cavaliers',
'id': 6,
'name': 'Cavaliers'},
'turnover': 7},
This is my code:
players_stats=[]
players_game_stats_url="https://balldontlie.io/api/v1/stats?season[]=2018&player_ids[]=237"
result=requests.get(players_game_stats_url).json()
for assists in result['data']:
players_stats.append({"Assists per game":assists['ast']})
What I would like to produce is something like this:
{Assists per game:9,8,6,7,3}
But what I'm getting is this:
[{'Assists per game': 9},
{'Assists per game': 8},
{'Assists per game': 6},
{'Assists per game': 7},
{'Assists per game': 3},
{'Assists per game': 9},
{'Assists per game': 4},
{'Assists per game': 7},
{'Assists per game': 3},
{'Assists per game': 8},
{'Assists per game': 8},
{'Assists per game': 8},
{'Assists per game': 2},
{'Assists per game': 4},
{'Assists per game': 9},
{'Assists per game': 7},
{'Assists per game': 7},
{'Assists per game': 5},
{'Assists per game': 8},
{'Assists per game': 5},
{'Assists per game': 3},
{'Assists per game': 9},
{'Assists per game': 4},
{'Assists per game': 6},
{'Assists per game': 3}]
CodePudding user response:
Based the comment from Kenny Ostrom
output = {"Assists per game": [game_info['ast'] for game_info in result['data']]}
which is going to result like you asked
{'Assists per game': [9, 8]}