Manu SS nair
Manu SS nair

Reputation: 25

Dictionary 'update' method

I have a script that builds multiple dictionaries and consolidate them as a single dictionary to return to the calling entity. The requirement is to append each dictionary at the end of the previous one. While I was building it in my PC (Windows 10, python 3.x), it worked fine as below.

{
  "Array Name": "SU73ARWVSPF01",
  "storageSystemId": "22186",
  "storageSystemName": "POD5_SU73ARWVSPF01",
  "accessible": true,
  "model": "VSP G1500",
  "svpIpAddress": "10.185.35.37",
  "firmwareVersion": "80-06-78-00/00",
  "lastRefreshedTime": "2020-12-21 14:45:31",
  "Pool List": {
     "Pool-1": {
      "storagePoolId": 11,
      "label": "DATA",
      "capacityInBytes": 590323982008320,
      "usedCapacityInBytes": 422152148877312,
      "availableCapacityInBytes": 168171833131008,
      "usedSubscription": 83
    },
      "Pool-2": {
      "storagePoolId": 12,
      "label": "LOGS",
      "capacityInBytes": 28142827732992,
      "usedCapacityInBytes": 21991601995776,
      "availableCapacityInBytes": 6151225737216,
      "usedSubscription": 78
    }
  },
  "SNMP Manager List": {
    "SNMP-1": {
      "name": "Test",
      "ipAddress": "1.1.1.1"
    },
    "SNMP-2": {
      "name": "Test1",
      "ipAddress": "2.2.2.2"
    }
   },
  "Hardware Alert List": {
    "diskAlerts": false,
    "powerSupplyAlerts": false,
    "batteryAlerts": false,
    "fanAlerts": false,
    "portAlerts": false,
    "cacheAlerts": false,
    "memoryAlerts": false,
    "processorAlerts": false
  }
}

However, after moving it to the server where the actual program will run (RHEL 7, python 2.7), the order of the dictionary is getting messed up as below.

{
  "accessible": true,
  "storageSystemName": "POD5_SU73ARWVSPF01",
  "Hardware Alert List": {
    "cacheAlerts": false,
    "powerSupplyAlerts": false,
    "portAlerts": false,
    "processorAlerts": false,
    "batteryAlerts": false,
    "diskAlerts": false,
    "fanAlerts": false,
    "memoryAlerts": false
  },
  "SNMP Manager List": {
    "SNMP-1": {
      "ipAddress": "1.1.1.1",
      "name": "Test"
    },
    "SNMP-2": {
      "ipAddress": "2.2.2.2",
      "name": "Test1"
    }
  },
  "svpIpAddress": "10.185.35.37",
  "storageSystemId": "22186",
  "Array Name": "SU73ARWVSPF01",
  "lastRefreshedTime": "2020-12-21 21:45:31",
  "model": "VSP G1500",
  "Pool List": {
    "Pool-2": {
      "usedSubscription": 78,
      "label": "LOGS",
      "usedCapacityInBytes": 21991601995776,
      "storagePoolId": 12,
      "availableCapacityInBytes": 6151225737216,
      "capacityInBytes": 28142827732992
    },
    "Pool-1": {
      "usedSubscription": 83,
      "label": "DATA",
      "usedCapacityInBytes": 422152148877312,
      "storagePoolId": 11,
      "availableCapacityInBytes": 168171833131008,
      "capacityInBytes": 590323982008320
    }
  },
  "firmwareVersion": "80-06-78-00/00"
}

The output from the first one is how I wanted and programmed it, by using the

OUTPUT={}
OUTPUT.update(new Dict1), OUTPUT.update(new Dict2).... etc

Is there a way to avoid the dictionaries getting inserted in the middle of the other one rather than at the end of it

Upvotes: 0

Views: 105

Answers (1)

luuk
luuk

Reputation: 1855

Python dictionaries maintaining their insertion order have only been a feature since Python 3.6 (although they were only an implementation detail back then; alternative Python implementations didn't have to adhere to this). Starting from Python 3.7, this has become a language specification.

Because you are using Python 2.7 on your server, dictionary order is not guaranteed using the default dict class. You can use the collections.OrderedDict class to ensure insertion order is remembered.

Upvotes: 3

Related Questions