I am trying to create a dictionary(sums), whose keys are the total sum of a list and value is the list itself. This list has a varying number of elements but the same name. Each time the elements of the list are changed and this changed list(same name) is assigned to a different key, the previously assigned value to key also changes.
While looking for some suitable answer to this, I learned from here(https://stackoverflow.com/a/52806573/7198441) that this could be because Python also references lists(Please correct me if am making an error here). Now, I want to know, if there is a way that I can assign a list having the same name but different elements at different stages of iteration to different keys of the same dictionary.
l = [1, 2, 3, 4, 5]
sums = {}
while len(l) :
l_sum = sum(l)
sums[l_sum] = l
print(sums)
print()
l.pop()
Actual result:
{15: [1, 2, 3, 4, 5]}
{15: [1, 2, 3, 4], 10: [1, 2, 3, 4]}
{15: [1, 2, 3], 10: [1, 2, 3], 6: [1, 2, 3]}
{15: [1, 2], 10: [1, 2], 6: [1, 2], 3: [1, 2]}
{15: [1], 10: [1], 6: [1], 3: [1], 1: [1]}
Expected:
{15: [1, 2, 3, 4, 5]}
{15: [1, 2, 3, 4, 5], 10: [1, 2, 3, 4]}
{15: [1, 2, 3, 4, 5], 10: [1, 2, 3, 4], 6: [1, 2, 3]}
{15: [1, 2, 3, 4, 5], 10: [1, 2, 3, 4], 6: [1, 2, 3], 3: [1, 2]}
{15: [1, 2, 3, 4, 5], 10: [1, 2, 3, 4], 6: [1, 2, 3], 3: [1, 2], 1: [1]}