Home > Mobile >  Python is adding to dictionary value of a key that is not being accessed
Python is adding to dictionary value of a key that is not being accessed

Time:06-19

My python program is adding data to a key that is not being accessed. I am accessing a key called "one_week" who's value is a dictionary. However when modifying the data in "one_week", the data in another key in the same dictionary as "one_week" is also being modified.

I have this data:

performance = {'2022-06-18': {'utilitiesChangesPercentage': -0.3293, 'basicMaterialsChangesPercentage': -0.6707, 'communicationServicesChangesPercentage': 1.5904, 'conglomeratesChangesPercentage': 0, 'consumerCyclicalChangesPercentage': 2.1271, 'consumerDefensiveChangesPercentage': 0.8003, 'energyChangesPercentage': -4.5986, 'financialChangesPercentage': -0.6562, 'financialServicesChangesPercentage': 0.4154, 'healthcareChangesPercentage': 2.3434, 'industrialsChangesPercentage': 0.5374, 'realEstateChangesPercentage': 1.2594, 'servicesChangesPercentage': 0, 'technologyChangesPercentage': 2.3738}, '2022-06-17': {'utilitiesChangesPercentage': -0.3293, 'basicMaterialsChangesPercentage': -0.6707, 'communicationServicesChangesPercentage': 1.5904, 'conglomeratesChangesPercentage': 0, 'consumerCyclicalChangesPercentage': 2.1271, 'consumerDefensiveChangesPercentage': 0.8003, 'energyChangesPercentage': -4.5986, 'financialChangesPercentage': -0.6562, 'financialServicesChangesPercentage': 0.4154, 'healthcareChangesPercentage': 2.3434, 'industrialsChangesPercentage': 0.5374, 'realEstateChangesPercentage': 1.2594, 'servicesChangesPercentage': 0, 'technologyChangesPercentage': 2.3738}, '2022-06-16': {'utilitiesChangesPercentage': -1.6436, 'basicMaterialsChangesPercentage': -3.4558, 'communicationServicesChangesPercentage': -4.5108, 'conglomeratesChangesPercentage': 0, 'consumerCyclicalChangesPercentage': -6.0633, 'consumerDefensiveChangesPercentage': -2.1755, 'energyChangesPercentage': -4.881, 'financialChangesPercentage': -3.9113, 'financialServicesChangesPercentage': -2.1979, 'healthcareChangesPercentage': -3.2739, 'industrialsChangesPercentage': -4.8727, 'realEstateChangesPercentage': -3.0626, 'servicesChangesPercentage': 0, 'technologyChangesPercentage': -4.849}, '2022-06-15': {'utilitiesChangesPercentage': 1.3948, 'basicMaterialsChangesPercentage': 1.2233, 'communicationServicesChangesPercentage': 2.6448, 'conglomeratesChangesPercentage': 0, 'consumerCyclicalChangesPercentage': 1.9838, 'consumerDefensiveChangesPercentage': 1.8249, 'energyChangesPercentage': -0.0898, 'financialChangesPercentage': 1.2547, 'financialServicesChangesPercentage': 0.5181, 'healthcareChangesPercentage': 1.7891, 'industrialsChangesPercentage': 1.5349, 'realEstateChangesPercentage': 1.5974, 'servicesChangesPercentage': 0, 'technologyChangesPercentage': 2.387}, '2022-06-14': {'utilitiesChangesPercentage': -1.5321, 'basicMaterialsChangesPercentage': -0.9762, 'communicationServicesChangesPercentage': -0.7266, 'conglomeratesChangesPercentage': 0, 'consumerCyclicalChangesPercentage': -0.079, 'consumerDefensiveChangesPercentage': 0.5071, 'energyChangesPercentage': -1.5016, 'financialChangesPercentage': -4.3202, 'financialServicesChangesPercentage': -0.3531, 'healthcareChangesPercentage': -0.7608, 'industrialsChangesPercentage': -0.5548, 'realEstateChangesPercentage': -1.217, 'servicesChangesPercentage': 0, 'technologyChangesPercentage': -0.376}, '2022-06-13': {'utilitiesChangesPercentage': -5.0062, 'basicMaterialsChangesPercentage': -6.0283, 'communicationServicesChangesPercentage': -5.8389, 'conglomeratesChangesPercentage': 0, 'consumerCyclicalChangesPercentage': -6.1994, 'consumerDefensiveChangesPercentage': -4.1023, 'energyChangesPercentage': -6.7057, 'financialChangesPercentage': -5.8465, 'financialServicesChangesPercentage': -2.5436, 'healthcareChangesPercentage': -5.8052, 'industrialsChangesPercentage': -4.9893, 'realEstateChangesPercentage': -5.4045, 'servicesChangesPercentage': 0, 'technologyChangesPercentage': -6.3218}, '2022-06-11': {'utilitiesChangesPercentage': -1.4438, 'basicMaterialsChangesPercentage': -0.8835, 'communicationServicesChangesPercentage': -2.7662, 'conglomeratesChangesPercentage': 0, 'consumerCyclicalChangesPercentage': -3.9916, 'consumerDefensiveChangesPercentage': -1.7072, 'energyChangesPercentage': -1.6846, 'financialChangesPercentage': -2.4166, 'financialServicesChangesPercentage': -1.3287, 'healthcareChangesPercentage': -4.0447, 'industrialsChangesPercentage': -2.9806, 'realEstateChangesPercentage': -1.9607, 'servicesChangesPercentage': 0, 'technologyChangesPercentage': -3.4835}, '2022-06-10': {'utilitiesChangesPercentage': -1.4438, 'basicMaterialsChangesPercentage': -0.8835, 'communicationServicesChangesPercentage': -2.7662, 'conglomeratesChangesPercentage': 0, 'consumerCyclicalChangesPercentage': -3.9916, 'consumerDefensiveChangesPercentage': -1.7072, 'energyChangesPercentage': -1.6846, 'financialChangesPercentage': -2.4166, 'financialServicesChangesPercentage': -1.3287, 'healthcareChangesPercentage': -4.0447, 'industrialsChangesPercentage': -2.9806, 'realEstateChangesPercentage': -1.9476, 'servicesChangesPercentage': 0, 'technologyChangesPercentage': -3.4835}, '2022-06-09': {'utilitiesChangesPercentage': -2.4094, 'basicMaterialsChangesPercentage': -3.0877, 'communicationServicesChangesPercentage': -2.38, 'conglomeratesChangesPercentage': 0, 'consumerCyclicalChangesPercentage': -2.2434, 'consumerDefensiveChangesPercentage': -1.9428, 'energyChangesPercentage': -2.5797, 'financialChangesPercentage': -2.1158, 'financialServicesChangesPercentage': -1.1578, 'healthcareChangesPercentage': -2.6882, 'industrialsChangesPercentage': -2.1356, 'realEstateChangesPercentage': -1.7272, 'servicesChangesPercentage': 0, 'technologyChangesPercentage': -3.392}, '2022-06-08': {'utilitiesChangesPercentage': -1.063, 'basicMaterialsChangesPercentage': -1.1986, 'communicationServicesChangesPercentage': 0.1896, 'conglomeratesChangesPercentage': 0, 'consumerCyclicalChangesPercentage': -0.1521, 'consumerDefensiveChangesPercentage': 0.0894, 'energyChangesPercentage': -0.5774, 'financialChangesPercentage': -1.9629, 'financialServicesChangesPercentage': -0.629, 'healthcareChangesPercentage': 0.4692, 'industrialsChangesPercentage': -1.4459, 'realEstateChangesPercentage': -1.288, 'servicesChangesPercentage': 0, 'technologyChangesPercentage': -0.6647}, '2022-06-07': {'utilitiesChangesPercentage': 0.0621, 'basicMaterialsChangesPercentage': 0.6928, 'communicationServicesChangesPercentage': 0.6454, 'conglomeratesChangesPercentage': 0, 'consumerCyclicalChangesPercentage': 0.6169, 'consumerDefensiveChangesPercentage': 0.3133, 'energyChangesPercentage': 2.9692, 'financialChangesPercentage': 0.8556, 'financialServicesChangesPercentage': 0.1918, 'healthcareChangesPercentage': 3.0295, 'industrialsChangesPercentage': 0.7845, 'realEstateChangesPercentage': 0.806, 'servicesChangesPercentage': 0, 'technologyChangesPercentage': 0.7692}, '2022-06-06': {'utilitiesChangesPercentage': -0.032, 'basicMaterialsChangesPercentage': 0.0809, 'communicationServicesChangesPercentage': -0.4309, 'conglomeratesChangesPercentage': 0, 'consumerCyclicalChangesPercentage': 0.3418, 'consumerDefensiveChangesPercentage': 0.2364, 'energyChangesPercentage': 0.1332, 'financialChangesPercentage': 0.863, 'financialServicesChangesPercentage': 0.0877, 'healthcareChangesPercentage': -2.3642, 'industrialsChangesPercentage': 0.6835, 'realEstateChangesPercentage': -0.2208, 'servicesChangesPercentage': 0, 'technologyChangesPercentage': -0.0487}, '2022-06-04': {'utilitiesChangesPercentage': -0.3472, 'basicMaterialsChangesPercentage': -1.5836, 'communicationServicesChangesPercentage': -1.581, 'conglomeratesChangesPercentage': 0, 'consumerCyclicalChangesPercentage': -1.6655, 'consumerDefensiveChangesPercentage': -0.5413, 'energyChangesPercentage': 0.9082, 'financialChangesPercentage': -1.197, 'financialServicesChangesPercentage': -0.6514, 'healthcareChangesPercentage': 0.5676, 'industrialsChangesPercentage': -0.6715, 'realEstateChangesPercentage': -0.8244, 'servicesChangesPercentage': 0, 'technologyChangesPercentage': -1.7852}, '2022-06-03': {'utilitiesChangesPercentage': -0.3472, 'basicMaterialsChangesPercentage': -1.5836, 'communicationServicesChangesPercentage': -1.581, 'conglomeratesChangesPercentage': 0, 'consumerCyclicalChangesPercentage': -1.6655, 'consumerDefensiveChangesPercentage': -0.5413, 'energyChangesPercentage': 0.9082, 'financialChangesPercentage': -1.197, 'financialServicesChangesPercentage': -0.6514, 'healthcareChangesPercentage': 0.5676, 'industrialsChangesPercentage': -0.6715, 'realEstateChangesPercentage': -0.8244, 'servicesChangesPercentage': 0, 'technologyChangesPercentage': -1.7852}, '2022-06-02': {'utilitiesChangesPercentage': 1.1764, 'basicMaterialsChangesPercentage': 3.4834, 'communicationServicesChangesPercentage': 1.7224, 'conglomeratesChangesPercentage': 0, 'consumerCyclicalChangesPercentage': 2.7236, 'consumerDefensiveChangesPercentage': 1.6454, 'energyChangesPercentage': 0.5619, 'financialChangesPercentage': 0.6452, 'financialServicesChangesPercentage': 0.7528, 'healthcareChangesPercentage': 2.1004, 'industrialsChangesPercentage': 2.3525, 'realEstateChangesPercentage': 0.669, 'servicesChangesPercentage': 0, 'technologyChangesPercentage': 3.3993}, '2022-06-01': {'utilitiesChangesPercentage': -0.3783, 'basicMaterialsChangesPercentage': -0.6145, 'communicationServicesChangesPercentage': -1.019, 'conglomeratesChangesPercentage': 0, 'consumerCyclicalChangesPercentage': -1.3616, 'consumerDefensiveChangesPercentage': -1.3518, 'energyChangesPercentage': 2.0396, 'financialChangesPercentage': -0.5411, 'financialServicesChangesPercentage': -0.3852, 'healthcareChangesPercentage': -2.302, 'industrialsChangesPercentage': -0.4424, 'realEstateChangesPercentage': -0.1271, 'servicesChangesPercentage': 0, 'technologyChangesPercentage': -1.0108}, '2022-05-31': {'utilitiesChangesPercentage': -0.8095, 'basicMaterialsChangesPercentage': -1.8391, 'communicationServicesChangesPercentage': -0.72, 'conglomeratesChangesPercentage': 0, 'consumerCyclicalChangesPercentage': -0.5211, 'consumerDefensiveChangesPercentage': -0.2501, 'energyChangesPercentage': -0.6778, 'financialChangesPercentage': 0.7535, 'financialServicesChangesPercentage': -0.1327, 'healthcareChangesPercentage': -1.3749, 'industrialsChangesPercentage': -0.6523, 'realEstateChangesPercentage': -0.2081, 'servicesChangesPercentage': 0, 'technologyChangesPercentage': -1.0196}, '2022-05-30': {'utilitiesChangesPercentage': 1.1453, 'basicMaterialsChangesPercentage': 1.8397, 'communicationServicesChangesPercentage': 1.9118, 'conglomeratesChangesPercentage': 0, 'consumerCyclicalChangesPercentage': 2.3831, 'consumerDefensiveChangesPercentage': 1.749, 'energyChangesPercentage': 1.9711, 'financialChangesPercentage': 0.5423, 'financialServicesChangesPercentage': 1.0953, 'healthcareChangesPercentage': 3.053, 'industrialsChangesPercentage': 2.187, 'realEstateChangesPercentage': 1.7737, 'servicesChangesPercentage': 0, 'technologyChangesPercentage': 3.3629}, '2022-05-28': {'utilitiesChangesPercentage': 1.1453, 'basicMaterialsChangesPercentage': 1.8731, 'communicationServicesChangesPercentage': 1.8654, 'conglomeratesChangesPercentage': 0, 'consumerCyclicalChangesPercentage': 2.3831, 'consumerDefensiveChangesPercentage': 1.749, 'energyChangesPercentage': 1.9711, 'financialChangesPercentage': 0.5423, 'financialServicesChangesPercentage': 1.0966, 'healthcareChangesPercentage': 3.053, 'industrialsChangesPercentage': 2.187, 'realEstateChangesPercentage': 1.7769, 'servicesChangesPercentage': 0, 'technologyChangesPercentage': 3.3629}, '2022-05-27': {'utilitiesChangesPercentage': 1.1453, 'basicMaterialsChangesPercentage': 1.8731, 'communicationServicesChangesPercentage': 1.8654, 'conglomeratesChangesPercentage': 0, 'consumerCyclicalChangesPercentage': 2.3831, 'consumerDefensiveChangesPercentage': 1.749, 'energyChangesPercentage': 1.9711, 'financialChangesPercentage': 0.5423, 'financialServicesChangesPercentage': 1.0966, 'healthcareChangesPercentage': 3.053, 'industrialsChangesPercentage': 2.187, 'realEstateChangesPercentage': 1.7737, 'servicesChangesPercentage': 0, 'technologyChangesPercentage': 3.3629}, '2022-05-26': {'utilitiesChangesPercentage': 0.3186, 'basicMaterialsChangesPercentage': 1.694, 'communicationServicesChangesPercentage': 2.3833, 'conglomeratesChangesPercentage': 0, 'consumerCyclicalChangesPercentage': 3.7358, 'consumerDefensiveChangesPercentage': 1.7021, 'energyChangesPercentage': 1.6321, 'financialChangesPercentage': 2.181, 'financialServicesChangesPercentage': 1.0564, 'healthcareChangesPercentage': 1.1553, 'industrialsChangesPercentage': 2.021, 'realEstateChangesPercentage': 0.9909, 'servicesChangesPercentage': 0, 'technologyChangesPercentage': 2.489}, '2022-05-25': {'utilitiesChangesPercentage': 0.7255, 'basicMaterialsChangesPercentage': 0.5966, 'communicationServicesChangesPercentage': 2.2651, 'conglomeratesChangesPercentage': 0, 'consumerCyclicalChangesPercentage': 3.5155, 'consumerDefensiveChangesPercentage': 1.2746, 'energyChangesPercentage': 2.6591, 'financialChangesPercentage': -0.3344, 'financialServicesChangesPercentage': 0.7993, 'healthcareChangesPercentage': 0.1801, 'industrialsChangesPercentage': 1.5138, 'realEstateChangesPercentage': 1.0937, 'servicesChangesPercentage': 0, 'technologyChangesPercentage': 2.2088}}

This is the code:

req = requests.get(self.sector_performance).json()
performance = {}
for day in req:
    performance[day.get('date')] = {}
    for item in day:
        if item != 'date':
            performance[day.get('date')][item] = day.get(item)
            if performance[day.get('date')][item] == None:
                performance[day.get('date')][item] = 0
today = datetime.date.today()
average_performance = {'one_day': {}, 'one_week': {}, 'two_week': {}, 'one_month': {}}
for i in range(len(performance)):
    str_today = str(today)
    if i == 0:
        average_performance['one_day'] = performance.get(str_today)
        average_performance['one_week'] = performance.get(str_today)
        average_performance['two_week'] = performance.get(str_today)
        average_performance['one_month'] = performance.get(str_today)
        print(average_performance)
    if i < 5:
        for item in performance.get(str_today):
            average_performance['one_week'][item]  = performance[str_today][item]
            print(average_performance)

This is the first two lines of output from the print statements:

{'one_day': {'utilitiesChangesPercentage': -0.3293, 'basicMaterialsChangesPercentage': -0.6707, 'communicationServicesChangesPercentage': 1.5904, 'conglomeratesChangesPercentage': 0, 'consumerCyclicalChangesPercentage': 2.1271, 'consumerDefensiveChangesPercentage': 0.8003, 'energyChangesPercentage': -4.5986, 'financialChangesPercentage': -0.6562, 'financialServicesChangesPercentage': 0.4154, 'healthcareChangesPercentage': 2.3434, 'industrialsChangesPercentage': 0.5374, 'realEstateChangesPercentage': 1.2594, 'servicesChangesPercentage': 0, 'technologyChangesPercentage': 2.3738}, 'one_week': {'utilitiesChangesPercentage': -0.3293, 'basicMaterialsChangesPercentage': -0.6707, 'communicationServicesChangesPercentage': 1.5904, 'conglomeratesChangesPercentage': 0, 'consumerCyclicalChangesPercentage': 2.1271, 'consumerDefensiveChangesPercentage': 0.8003, 'energyChangesPercentage': -4.5986, 'financialChangesPercentage': -0.6562, 'financialServicesChangesPercentage': 0.4154, 'healthcareChangesPercentage': 2.3434, 'industrialsChangesPercentage': 0.5374, 'realEstateChangesPercentage': 1.2594, 'servicesChangesPercentage': 0, 'technologyChangesPercentage': 2.3738}, 'two_week': {'utilitiesChangesPercentage': -0.3293, 'basicMaterialsChangesPercentage': -0.6707, 'communicationServicesChangesPercentage': 1.5904, 'conglomeratesChangesPercentage': 0, 'consumerCyclicalChangesPercentage': 2.1271, 'consumerDefensiveChangesPercentage': 0.8003, 'energyChangesPercentage': -4.5986, 'financialChangesPercentage': -0.6562, 'financialServicesChangesPercentage': 0.4154, 'healthcareChangesPercentage': 2.3434, 'industrialsChangesPercentage': 0.5374, 'realEstateChangesPercentage': 1.2594, 'servicesChangesPercentage': 0, 'technologyChangesPercentage': 2.3738}, 'one_month': {'utilitiesChangesPercentage': -0.3293, 'basicMaterialsChangesPercentage': -0.6707, 'communicationServicesChangesPercentage': 1.5904, 'conglomeratesChangesPercentage': 0, 'consumerCyclicalChangesPercentage': 2.1271, 'consumerDefensiveChangesPercentage': 0.8003, 'energyChangesPercentage': -4.5986, 'financialChangesPercentage': -0.6562, 'financialServicesChangesPercentage': 0.4154, 'healthcareChangesPercentage': 2.3434, 'industrialsChangesPercentage': 0.5374, 'realEstateChangesPercentage': 1.2594, 'servicesChangesPercentage': 0, 'technologyChangesPercentage': 2.3738}}

and

{'one_day': {'utilitiesChangesPercentage': -0.6586, 'basicMaterialsChangesPercentage': -0.6707, 'communicationServicesChangesPercentage': 1.5904, 'conglomeratesChangesPercentage': 0, 'consumerCyclicalChangesPercentage': 2.1271, 'consumerDefensiveChangesPercentage': 0.8003, 'energyChangesPercentage': -4.5986, 'financialChangesPercentage': -0.6562, 'financialServicesChangesPercentage': 0.4154, 'healthcareChangesPercentage': 2.3434, 'industrialsChangesPercentage': 0.5374, 'realEstateChangesPercentage': 1.2594, 'servicesChangesPercentage': 0, 'technologyChangesPercentage': 2.3738}, 'one_week': {'utilitiesChangesPercentage': -0.6586, 'basicMaterialsChangesPercentage': -0.6707, 'communicationServicesChangesPercentage': 1.5904, 'conglomeratesChangesPercentage': 0, 'consumerCyclicalChangesPercentage': 2.1271, 'consumerDefensiveChangesPercentage': 0.8003, 'energyChangesPercentage': -4.5986, 'financialChangesPercentage': -0.6562, 'financialServicesChangesPercentage': 0.4154, 'healthcareChangesPercentage': 2.3434, 'industrialsChangesPercentage': 0.5374, 'realEstateChangesPercentage': 1.2594, 'servicesChangesPercentage': 0, 'technologyChangesPercentage': 2.3738}, 'two_week': {'utilitiesChangesPercentage': -0.6586, 'basicMaterialsChangesPercentage': -0.6707, 'communicationServicesChangesPercentage': 1.5904, 'conglomeratesChangesPercentage': 0, 'consumerCyclicalChangesPercentage': 2.1271, 'consumerDefensiveChangesPercentage': 0.8003, 'energyChangesPercentage': -4.5986, 'financialChangesPercentage': -0.6562, 'financialServicesChangesPercentage': 0.4154, 'healthcareChangesPercentage': 2.3434, 'industrialsChangesPercentage': 0.5374, 'realEstateChangesPercentage': 1.2594, 'servicesChangesPercentage': 0, 'technologyChangesPercentage': 2.3738}, 'one_month': {'utilitiesChangesPercentage': -0.6586, 'basicMaterialsChangesPercentage': -0.6707, 'communicationServicesChangesPercentage': 1.5904, 'conglomeratesChangesPercentage': 0, 'consumerCyclicalChangesPercentage': 2.1271, 'consumerDefensiveChangesPercentage': 0.8003, 'energyChangesPercentage': -4.5986, 'financialChangesPercentage': -0.6562, 'financialServicesChangesPercentage': 0.4154, 'healthcareChangesPercentage': 2.3434, 'industrialsChangesPercentage': 0.5374, 'realEstateChangesPercentage': 1.2594, 'servicesChangesPercentage': 0, 'technologyChangesPercentage': 2.3738}}

From my understanding, the "one_day" key should not be modified after its first modification under the statement if i == 0:. However, it is being modified under the statement if i < 5:.

CodePudding user response:

No, you do NOT want "the same object" to be assigned to all four keys. That is the cause of your problem. When you modify one_week, you are modifying that one object (INCLUDING the one referenced in the original performance dict), and the change will be seen in all of the keys.

Since you created empty dictionaries for those keys, the easy solution is to use "update" to update the separate empty dictionary ojbects:

    if i == 0:
        average_performance['one_day'].update(performance.get(str_today))
        average_performance['one_week'].update(performance.get(str_today))
        average_performance['two_week'].update(performance.get(str_today))
        average_performance['one_month'].update(performance.get(str_today))
        print(average_performance)
  • Related