0

This block of code pulls in data from an API as a dict and fields are a list of the keys we want to find values for. In the function getTeamData it passes through the API data creating teams_list which is the returned value. The nested for loops are iterating over the dict at each index and storing each value in temp_list.

In the nested for loop I am appending each value found to create a temporary list in temp_list. This is where I am having a disconnect, when the inner loop completes, we then empty temp_list but when we then append temp_list to teams_list we still get the expected results.

The only thing I can think of but still don't quite understand is since we are not deleting the data from temp_list and just changing the reference to an empty list, somehow the data is still being held onto by something?

import statsapi

# Stores Team data as dict
teams = statsapi.get('teams', {'sportIds': 1})['teams']

# List of keys' values we are trying to retreive 
fields = ['id', 'name', 'abbreviation', 'teamName', 'locationName', 'firstYearOfPlay']


# iterates over the team dict and pulls pre-defined values based on fields list
def getTeamData(dict):
    teams_list = list()
    for i in range(len(dict)):
        print("temp list emptied")
        temp_list = []
        print(temp_list)
        print("adding temp list to teams_list")
        teams_list.append(temp_list)
        print(teams_list)
        for k in range(len(fields)):
            print("adding value to temp list")
            temp_list.append(str(dict[i].get(fields[k])))
            print(temp_list)
        return teams_list


print(getTeamData(teams))
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
z3ke
  • 35
  • 3
  • First thing first, is there an indentation problem for the `return` statement? It seems to return at the end of the first `i` loop. – lpounng May 29 '21 at 05:47
  • This was formatting from the browser I over looked, the return is not supposed to be within the loop – z3ke May 29 '21 at 05:50
  • Can you post a sample "teams" dictionary for us to run the function and debug your code? – Ganesh Tata May 29 '21 at 05:50
  • I can't see how this would work but note that append() will only add an element to a list. To include a whole list, use extend() – Stefan May 29 '21 at 05:55

2 Answers2

1

we then append temp_list to teams_list we still get the expected results.

You still get the expected results because when you append temp_list to teams_list, you are appending a reference of temp_list. Hence, when changes are made to temp_list, it is also reflected in teams_list. From your code, it seems like you are making changes to temp_list in the for k in range(len(fields)): loop.

For more info regarding how to workaround this issue (if you need to), checkout this answer. This answer might also be helpful in understanding references in python.

Ganesh Tata
  • 1,118
  • 8
  • 26
0

I have aligned the return line, and re-arranged some codes especially teams_list.append(temp_list) to the logical position

Will this work?

def getTeamData(dic):
    teams_list = []
    for i in range(len(dic)):
        print("temp list emptied")
        temp_list = []
        print(temp_list)
        for k in range(len(fields)):
            print("adding value to temp list")
            temp_list.append(str(dict[i].get(fields[k])))
            print(temp_list)
            print("adding temp list to teams_list")
            teams_list.append(temp_list)
            print(teams_list)
    return teams_list
MiH
  • 354
  • 4
  • 11