2

I am trying to append dictionaries to a list here, problem is after appending both "Chrome" and "Firefox" values to the "list" i see only firefox.exe in list for both entries ..

any help is really appreciated. See the print statement of dictionary where both values are different.

MyItems = ["ChromeSetup.exe","firefox.exe"]
listofitems = [{"appId": "ChromeID", 'id': "0","name": 'ChromeSetup.exe','_id': 'ChromeUnique'},{"appId": "FireFoxID", 'id': "0","name": 'firefox.exe','_id': 'FireFoxUnique'} ]

__id = ""
appId = ""
result = []
Dict = {"installerParameters":"","managedApp":{"_id":__id, "appId":appId},"postInstallAction":0,"postInstallScript":{"_id":"0"},"preInstallScript":{"_id":"0"}}

for app in MyItems:
    for items in listofitems:
        if items['name'] == app:
            Dict["managedApp"]["_id"] = items['_id']
            Dict["managedApp"]["appId"] = items['appId']
            print("Dictionery",Dict)
            result.append(Dict)
            break

print("See the List", result)

Result:

Dictionery {'installerParameters': '', 'managedApp': {'_id': 'ChromeUnique', 'appId': 'ChromeID'}, 'postInstallAction': 0, 'postInstallScript': {'_id': '0'}, 'preInstallScript': {'_id': '0'}}
Dictionery {'installerParameters': '', 'managedApp': {'_id': 'FireFoxUnique', 'appId': 'FireFoxID'}, 'postInstallAction': 0, 'postInstallScript': {'_id': '0'}, 'preInstallScript': {'_id': '0'}}
See the List [{'installerParameters': '', 'managedApp': {'_id': 'FireFoxUnique', 'appId': 'FireFoxID'}, 'postInstallAction': 0, 'postInstallScript': {'_id': '0'}, 'preInstallScript': {'_id': '0'}}, {'installerParameters': '', 'managedApp': {'_id': 'FireFoxUnique', 'appId': 'FireFoxID'}, 'postInstallAction': 0, 'postInstallScript': {'_id': '0'}, 'preInstallScript': {'_id': '0'}}]
Ardra Madhu
  • 157
  • 1
  • 2
  • 7
  • 1
    You have one dictionary object and you are appending two references to that object to your list. – PM 2Ring Jul 14 '18 at 05:49
  • Also see http://stackoverflow.com/questions/2612802/how-to-clone-or-copy-a-list-in-python – PM 2Ring Jul 14 '18 at 07:42
  • @PM2Ring i believe this question is very clear and easy to understand compare to the question you have marked as duplicate. How ever im okay to delete this question if its not relevant. – Ardra Madhu Jul 17 '18 at 07:45
  • There's no need to delete this question. It's quite good. There's nothing wrong with asking a duplicate question. The problem is the duplicate _answers_ that get scattered all over the place instead of being in one central location. – PM 2Ring Jul 17 '18 at 07:49
  • got it .. that helps. – Ardra Madhu Jul 17 '18 at 07:59

2 Answers2

5

Define the dictionary in the for loop. You are currently writing to same dictionary object, and list holds reference to this object which itself is a reference. As a result you keep modifying the same object.

MyItems = ["ChromeSetup.exe","firefox.exe"]
listofitems = [{"appId": "ChromeID", 'id': "0","name": 'ChromeSetup.exe','_id': 'ChromeUnique'},{"appId": "FireFoxID", 'id': "0","name": 'firefox.exe','_id': 'FireFoxUnique'} ]

__id = ""
appId = ""
result = []

for app in MyItems:
    for items in listofitems:
        if items['name'] == app:
            # I would try to find a better var name.
            Dict = {"installerParameters":"","managedApp":{"_id":__id, "appId":appId},"postInstallAction":0,"postInstallScript":{"_id":"0"},"preInstallScript":{"_id":"0"}}
            Dict["managedApp"]["_id"] = items['_id']
            Dict["managedApp"]["appId"] = items['appId']
            print("Dictionery",Dict)
            result.append(Dict)
            break

print("See the List", result)
0xc0de
  • 8,028
  • 5
  • 49
  • 75
  • 2
    I agree that it would be better to have a more meaningful name than `Dict`. But anyway, another option is to define the basic `Dict` outside the loop, and then inside the loop create a copy of it using [`copy.deepcopy`](https://docs.python.org/3/library/copy.html#copy.deepcopy). If `Dict` didn't contain those nested dicts we could just use `Dict.copy`, but that won't work here, since we'd just get shared references to those inner dicts. – PM 2Ring Jul 14 '18 at 06:10
  • @PM2Ring Agreed, +1. – 0xc0de Jul 14 '18 at 06:55
3

Your dictionary object Dict is being overwritten during the second run of the loop. This is happening because you have defined the Dict above the loop. Better to define the Dict inside the loop.

Jay
  • 24,173
  • 25
  • 93
  • 141