Skip to content
Advertisement

list in dictionary get empty out when coming out of the loop

This is the code I wrote for web scraping purposes.

I want to save all data in the dictionary and then save that data into a dataframe.

Up to the last iteration, it saves the dictionary, but when coming out of the loop all lists (that are the values of my dictionary) are empty. How can one fix that?

JavaScript

This is output. In the output, I have printed list and dictionary state in each iteration

JavaScript

Advertisement

Answer

The problem in league_data[i]=team_points, after this line execution league_data[i] and team_points point to same object (as you see in my output, both have same id)

JavaScript
JavaScript

Solution: just modify this line league_data[i]=team_points to league_data[i]=team_points.copy(). and problem is solved

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement