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))