Why are my dictionary values changing after the loop?
Question: Following is my python code:

    
    
    from collections import defaultdict
    data = {"Manoj":{"India":{"Airtel":2000,"Vodafone":5000},"Pakistan":{"Airtel":3000}},
        "Shafiq":{"Pakistan":{"Airtel":5000,"BP":10000}},
        "Shams":{"India":{"BP":400}},
        "Govind":{"India":{"Airtel":3000,"Vodafone":2000}},
        "Zakir":{"SriLanka":{"Etisalat":7000}}
        }
    
    print("Printing data:")
    print(data)
    sumOperator={}
    countryCustomers = defaultdict(list)
    
    sum=0
    
    for name,value in data.items():
        for country,value2 in value.items():
            countryCustomers[country].append(name)
            if country not in sumOperator:
                sumOperator[country]=value2
            else:
                for operator,value3 in value2.items():
                    if operator not in sumOperator[country]:
                        sumOperator[country][operator] = value3
                    else:
                        sum = sumOperator[country][operator] + value3
                        sumOperator[country][operator] = sum
    
    print("")
    print("Printing sumOperator:")
    print(sumOperator)
    print("")
    print("Priniting data:")
    print(data)
    
    Output:
    Printing data:
    {'Zakir': {'SriLanka': {'Etisalat': 7000}}, 'Manoj': {'Pakistan': {'Airtel':3000}, 'India': {'Vodafone': 5000, 'Airtel': 2000}}, 'Shams': {'India': {'BP': 400}}, 'Shafiq': {'Pakistan': {'BP': 10000, 'Airtel': 5000}}, 'Govind': {'India': {'Vodafone': 2000, 'Airtel': 3000}}}
    
    Printing sumOperator:
    {'Pakistan': {'BP': 10000, 'Airtel': 8000}, 'SriLanka': {'Etisalat': 7000}, 'India': {'Vodafone': 7000, 'BP': 400, 'Airtel': 5000}}
    
    Priniting data:
    {'Zakir': {'SriLanka': {'Etisalat': 7000}}, 'Manoj': {'Pakistan': {'BP': 10000, 'Airtel': 8000}, 'India': {'Vodafone': 7000, 'BP': 400, 'Airtel': 5000}}, 'Shams': {'India': {'BP': 400}}, 'Shafiq': {'Pakistan': {'BP': 10000, 'Airtel': 5000}}, 'Govind': {'India': {'Vodafone': 2000, 'Airtel': 3000}}}
    

Why is my dictionary "data" changing values after initializing another
dictionary? The last print data statement produces another data dictionary, it
is not the same as the one initialized in the beginning


Answer: The problem happens when your program adds values to the _sumOperator_
dictionary, and mainly with the final `else` statement in your loop. You are
trying to build this dictionary from the values of another dictionary, so you
have `sumOperator[key] = value`, where `value` refers to an item in the other
dictionary. This simply points one dictionary to the values of another; the
underlying value isn't copied. Thus, when you update the values of
_sumOperator_ , the main _data_ dictionary also got updated. To fix this,
simply copy the values as you add them to the new dictionary.

The following code works, we simply add `copy()` whenever we update
_sumOperator_ :

    
    
    from collections import defaultdict
    from copy import copy
    data = {"Manoj":{"India":{"Airtel":2000,"Vodafone":5000},"Pakistan":{"Airtel":3000}},
            "Shafiq":{"Pakistan":{"Airtel":5000,"BP":10000}},
            "Shams":{"India":{"BP":400}},
            "Govind":{"India":{"Airtel":3000,"Vodafone":2000}},
            "Zakir":{"SriLanka":{"Etisalat":7000}}
            }
    
    print("Printing data:")
    print(data)
    
    sumOperator={}
    countryCustomers = defaultdict(list)
    
    sum=0
    
    for name,value in data.items():
        for country,value2 in value.items():
            countryCustomers[country].append(name)
            if country not in sumOperator:
                sumOperator[country] = copy(value2)
            else:
                for operator,value3 in value2.items():
                    if operator not in sumOperator[country]:
                        sumOperator[country][operator] = copy(value3)
                    else:
                        sum = sumOperator[country][operator] + copy(value3)
                        sumOperator[country][operator] = sum
    
    
    print("Printing sumOperator:")
    print(sumOperator)
    
    print("Priniting data:")
    print(data)
    

As others have pointed out, you may wish to view the Python doc pages on
[assignment statements and
copying](https://docs.python.org/3.4/library/copy.html). It is important to
keep in mind of when you need to create an explicit copy if you do not wish to
modify the original.

