This is a small snippet of a larger piece of code.
It’s supposed to put data into temp_dict and then add it to the full list of dictionaries list_of_dicts. Then it loops, clears temp_dict, then puts new data into temp_dict and adds that new dictionary to list_of_dicts.
So for this code example:
list_of_dicts = []
blank = {}
temp_dict = {}
for i in range(0,5):
temp_dict = blank
print(i)
temp_dict['key'] = i
list_of_dicts.append(temp_dict)
print(list_of_dicts)
The result should be:
0
1
2
3
4
[{'key': 0}, {'key':1},{'key':2},{'key':3},{'key':4}]
But instead I get:
0
1
2
3
4
[{'key': 4}, {'key': 4}, {'key': 4}, {'key': 4}, {'key': 4}]
I feel like there’s a really simple fix to this that I’m just not seeing.