API Data is not being added to my dictionary correctly

Question:

API: https://gist.github.com/nntrn/ee26cb2a0716de0947a0a4e9a157bc1c#v2sportsfootballleaguesnflseasonsyeartypesseasontypeweeksweeknumevents

Chosen API Link: https://sports.core.api.espn.com/v2/sports/football/leagues/nfl/seasons/2022/types/2/weeks/16/events

From this API, I am attempting to extract 12 pieces of data for each team (32 teams), so that I can plug those numbers into a formula and predict the winners of matchups. I created a dictionary to format how I want to store the data here: https://pastebin.com/cK1wJ0ZS (Teams are represented by their "id" in the index) Their ID is assigned in the API.

When I run my data extract file ( https://pastebin.com/LwGus277 ), 60 entries are entered into the categories I have written. This is coming from the loop in my fill_game() method. I do not understand why all 60 are coming through.

for game in range(len(games["items"])):
     self.fill_home_team(games["items"][game]["$ref"])

Each list’s length inside of my sub-categories should be equal to the last_games param of __init__, but it is 60 now.

If last_games was 1, then there would only be 1 week’s worth of games, and since each team plays once per week, there should be 1 piece of data per category.

If anyone could help that would be greatly appreciated!

I’ve gone through and followed the instructions line by line as if I was the computer, and still could not understand how these numbers were being looped in.

EDIT: I have tried debugging these lines in PyCharm:

    # Add Turnover Differential
    self.stats[team_id - 1][0]["Turnover Differential"].append(path[10]["stats"][39]["value"])

    # Add RedZone Efficiency
    self.stats[team_id - 1][1]["RedZone Efficiency"].append(path[10]["stats"][12]["value"])

    # Add Third Down Attempts
    self.stats[team_id - 1][2]["Third Down Attempts"].append(path[10]["stats"][29]["value"])

I noticed that instead of putting the team’s data in their respective index at [team_id – 1], it is appending the data to EVERY team’s list. I cannot figure out why that is happening. Maybe I’m overlooking something?

Asked By: Raylo

||

Answers:

I did some debugging and can understand why it gave you 60 entries because each week contains the following number of games,

enter image description here

Thus, 16 + 16 + 13 + 15 = 60

The issue is when you initialise the stats object,

change from

self.stats = [n for i in range(32)]

to

self.stats = [[
            {"Turnover Differential": []},  # 0
            {"RedZone Efficiency": []},  # 1
            {"Third Down Attempts": []},  # 2
            {"Third Down Conversions": []},  # 3
            {"Fourth Down Attempts": []},  # 4
            {"Fourth Down Conversions": []},  # 5
            {"Kickoff Yards": []},  # 6
            {"Kickoff Attempts": []},  # 7
            {"Kick Return Yards": []},  # 8
            {"Kick Return Attempts": []},  # 9
            {"Punt Yards": []},  # 10
            {"Punt Attempts": []},  # 11
        ] for i in range(32)]

The previous code gives you 32 references to the same object n (updating one will show the changes in all the items), whereas the new code gives you 32 distinct (independent) objects.

Answered By: Charles Han
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.