0

Given this list:

pc = ['OferteConfigurabile_S2D_Rate',
'OferteConfigurabile_S2D_Rate_si_Discount',
'OferteConfigurabile_S2D_SimOnly_si_Discount',
'OferteConfigurabile_S2D_SimOnly',
'OferteConfigurabile_S2D_VMM_Rate']

And this dictionary:

lst = []
dataModel = {
        'channel': 'RETAIL',
        'searchType': 'MSISDN',
        'searchValue': 727277696,
        'configType': 'RETENTIE',
        'scenarioName_PC': 'OferteConfigurabile_ServiceOnly',
        'retention_option': '360_OFERTE_MOBILE',
        'retention_flow': 'ConfigureazaOferte',
        }

I want for every element in the 'pc' list to update dateModel['scenarioName_PC'] and store the resulting dictionary into a list but slightly changed by creating a new dict with a custom key and dataModel dictionary as its value

for i in pc:
    dataModel['scenarioName_PC'] = i

    lst.append({f"{dataModel['retention_option']}_{dataModel['retention_flow']}_{i}" : dataModel})

print(lst)

The problem is that when i print the list 'scenarioName_PC' key always has the last element from the iterated list, the dataModel dictionary dosen't save the value for every for loop iteration, it somehow only stores the last value in PC list

     [

   {

      "360_OFERTE_MOBILE_ConfigureazaOferte_OferteConfigurabile_S2D_Rate":{

         "channel":"RETAIL",

         "searchType":"MSISDN",

         "searchValue":727277696,

         "configType":"RETENTIE",

         "scenarioName_PC":"OferteConfigurabile_S2D_VMM_Rate",

         "retention_option":"360_OFERTE_MOBILE",

         "retention_flow":"ConfigureazaOferte"

      }

   },

   {

      "360_OFERTE_MOBILE_ConfigureazaOferte_OferteConfigurabile_S2D_Rate_si_Discount":{

         "channel":"RETAIL",

         "searchType":"MSISDN",

         "searchValue":727277696,

         "configType":"RETENTIE",

         "scenarioName_PC":"OferteConfigurabile_S2D_VMM_Rate",

         "retention_option":"360_OFERTE_MOBILE",

         "retention_flow":"ConfigureazaOferte"

      }

   },

Expected result is a list with dataModel dictionary but for scenarioname_PC key to have every time 'i' as value. [

   {

      "360_OFERTE_MOBILE_ConfigureazaOferte_OferteConfigurabile_S2D_Rate":{

         "channel":"RETAIL",

         "searchType":"MSISDN",

         "searchValue":727277696,

         "configType":"RETENTIE",

         "scenarioName_PC":"OferteConfigurabile_S2D_Rate",

         "retention_option":"360_OFERTE_MOBILE",

         "retention_flow":"ConfigureazaOferte"

      }

   },

   {

      "360_OFERTE_MOBILE_ConfigureazaOferte_OferteConfigurabile_S2D_Rate_si_Discount":{

         "channel":"RETAIL",

         "searchType":"MSISDN",

         "searchValue":727277696,

         "configType":"RETENTIE",

         "scenarioName_PC":"OferteConfigurabile_S2D_Rate_si_Discount",

         "retention_option":"360_OFERTE_MOBILE",

         "retention_flow":"ConfigureazaOferte"

      }

   },
Rolfsky
  • 63
  • 7
  • Can you please post an example of your desired output? – artemis Oct 22 '20 at 12:17
  • 2
    "The problem is that when i print the list 'scenarioName_PC' key always has the last element from the iterated list, " - it is not a problem.. it is how dict works.. – balderman Oct 22 '20 at 12:18
  • OK, so I want to have the value in the dict updated for every for loop, with i, not only the last one in the list. @baldermanm – Rolfsky Oct 22 '20 at 12:20
  • @wundermahn, immediately. – Rolfsky Oct 22 '20 at 12:20
  • Does this answer your question? [List of lists changes reflected across sublists unexpectedly](https://stackoverflow.com/questions/240178/list-of-lists-changes-reflected-across-sublists-unexpectedly) – Tomerikoo Oct 22 '20 at 12:27
  • You are `append`ing every time **the same dict object**. When you change a key's value it is reflected in all elements of the list because the list just have the same dict object again and again – Tomerikoo Oct 22 '20 at 12:29

1 Answers1

0

while appending copy the dictionary object instead of just passing the dictionary. You are passing the dictionary reference which is being modified.

This should work.

import copy
for i in pc:
    new_dataMode = copy.deepcopy(dataMode)
    new_dataModel['scenarioName_PC'] = i

    lst.append({f"{new_dataModel['retention_option']}_{new_dataModel['retention_flow']}_{i}" : new_dataModel})

print(lst)
Atreyagaurav
  • 1,145
  • 6
  • 15