Search code examples
pythonpython-3.xpandasdictionarydata-analysis

Dict List Continues to Overwrite - What am I doing wrong?


I haven't had this problem before and have been trying to troubleshoot it for a while now but can't seem to figure out the issue (tried a variety of things including creating copies, deep copies, and appending to DataFrame).

Basically, I'm trying to loop through a list, create a dictionary and append that dictionary to a different list. The dictionary creation is unique each time, but it overwrites all of the previous ones AND adds it.

And sorry in advance if there is an obvious answer - still pretty new at this.

See below for code:

bigram_values_dict_list = []
bigram_values_dict = {}
counter = 0

for bigram in bigram_string_list:
    bigram_values_dict['bigram'] = bigram
    bigram_values_dict['impressions'] = get_total_impressions(bigram)

    print(bigram_values_dict)

    counter += 1

    if counter % 10 == 0:
        print(bigram_values_dict_list)

    bigram_values_dict_list.append(bigram_values_dict)

And output:

{'bigram': 'mobile site', 'impressions': 10344864}
{'bigram': 'learn more!', 'impressions': 4167059}
{'bigram': 'lawn &', 'impressions': 742291}
{'bigram': '& garden', 'impressions': 980153}
[{'bigram': '& garden', 'impressions': 2123500}, {'bigram': '& garden', 'impressions': 2123500}, {'bigram': '& garden', 'impressions': 2123500}, {'bigram': '& garden', 'impressions': 2123500}, {'bigram': '& garden', 'impressions': 2123500}, {'bigram': '& garden', 'impressions': 2123500}, {'bigram': '& garden', 'impressions': 2123500}, {'bigram': '& garden', 'impressions': 2123500}, {'bigram': '& garden', 'impressions': 2123500}]

I don't think I ever had this problem with Python 2. Maybe I'm missing something?

Thanks in advance for any help / insight!!


Solution

  • Change:

    bigram_values_dict_list.append(bigram_values_dict)
    

    to:

    bigram_values_dict_list.append(bigram_values_dict.copy())
    

    In that way, you are appending a copy of your dictionary to the list, so future modifications of the dictionary will not affect already appended dictionaries.

    Another alternative will be to (re-)define the dictionary at every iteration of the for loop.