Python Merge two dictionary without losing the order

  • I have a dictionary containing UUID generated for documents like below.
{
  "UUID": [
    "b8f2904b-dafd-4be3-9615-96bac8e16c7f",
    "1240ad39-4815-480f-8cb2-43f802ba8d4e"
  ]
}
  • And another dictionary as a nested one
{
  "R_Id": 304,
  "ContextKey": "Mr.Dave",
  "ConsolidationInformation": {
    "Input": [
      {
        "DocumentCode": "BS",
        "ObjectType": "Document",
        "InputContextKey": "Mr.Dave_HDFC.pdf2022-08-010T09:40:06.429358"
      },
      {
        "DocumentCode": "F16",
        "ObjectType": "Document",
        "InputContextKey": "Mr.Dave_F16.pdf2022-08-010T09:40:06.429358"
      }
    ]
  }
}
  • I want to add the UUID by index to the ['ConsolidationInformation']['Input'] and inside individual Input as DocumentUUID, how can I map it using a for loop. I tried searching on the internet but was not able to find a solution that can satisfy this condition of nested.
  • Expected output
{
  "R_Id": 304,
  "ContextKey": "Mr.Dave",
  "ConsolidationInformation": {
    "Input": [
      {
        "DocumentCode": "BS",
        "ObjectType": "Document",
        "InputContextKey": "Mr.Dave_HDFC.pdf2022-08-010T09:40:06.429358",
        "DocumentUUID": "b8f2904b-dafd-4be3-9615-96bac8e16c7f"
      },
      {
        "DocumentCode": "F16",
        "ObjectType": "Document",
        "InputContextKey": "Mr.Dave_F16.pdf2022-08-010T09:40:06.429358",
        "DocumentUUID": "1240ad39-4815-480f-8cb2-43f802ba8d4e"
      }
    ]
  }
}
  • I tried something like below, but resulted in
Traceback (most recent call last):
File "<string>", line 26, in <module>
KeyError: 0
  • Code
uuid = {
  "UUID": [
    "b8f2904b-dafd-4be3-9615-96bac8e16c7f",
    "1240ad39-4815-480f-8cb2-43f802ba8d4e"
  ]
}
document = {
  "R_Id": 304,
  "ContextKey": "Mr.Dave",
  "ConsolidationInformation": {
    "Input": [
      {
        "DocumentCode": "BS",
        "ObjectType": "Document",
        "InputContextKey": "Mr.Dave_HDFC.pdf2022-08-010T09:40:06.429358"
      },
      {
        "DocumentCode": "F16",
        "ObjectType": "Document",
        "InputContextKey": "Mr.Dave_F16.pdf2022-08-010T09:40:06.429358"
      }
    ]
  }
}
for i, document in enumerate(document):
    uuid = uuid[i]
    print(f"${uuid} for 1 {document}")

>Solution :

Issues: You have done well with your attempt. The only issue is that the values need to be accessed and added at the correct level of nesting.

Solution: You can correct your attempt as follows:

for i, doc in enumerate(document['ConsolidationInformation']['Input']):
    doc['DocumentUUID'] = uuid['UUID'][i]

Alternatively: You can use the zip function. You can learn more about this function here.

Here is an example of how you may apply the function to your code:

for u, doc in zip(uuid['UUID'], document['ConsolidationInformation']['Input']):
    doc['DocumentUUID'] = u

Output: The output is as follows:

{
   "R_Id":304,
   "ContextKey":"Mr.Dave",
   "ConsolidationInformation":{
      "Input":[
         {
            "DocumentCode":"BS",
            "ObjectType":"Document",
            "InputContextKey":"Mr.Dave_HDFC.pdf2022-08-010T09:40:06.429358",
            "DocumentUUID":"b8f2904b-dafd-4be3-9615-96bac8e16c7f"
         },
         {
            "DocumentCode":"F16",
            "ObjectType":"Document",
            "InputContextKey":"Mr.Dave_F16.pdf2022-08-010T09:40:06.429358",
            "DocumentUUID":"1240ad39-4815-480f-8cb2-43f802ba8d4e"
         }
      ]
   }
}

Leave a Reply