Python Merge two dictionary without losing the order

Question:

  • 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 a loop. I tried searching on the internet but could not find a solution that could satisfy this nested condition.
  • 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 the below, but it 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}")
Asked By: donny

||

Answers:

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

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"
         }
      ]
   }
}
Answered By: Prins

You need to go a little deeper in to your dictionaries;

for u,d in zip(uuid['UUID'], document['ConsolidationInformation']['Input']):
    d['UUID'] = u
Answered By: bn_ln

Here’s my solution that works using a for – enumerate without using any libraries. Please edit it to best suit your needs!

  "UUID": [
    "b8f2904b-dafd-4be3-9615-96bac8e16c7f",
    "1240ad39-4815-480f-8cb2-43f802ba8d4e"
  ]
}
Nested_Dict = {
  "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 a,i in enumerate(UUID_Dict['UUID']):
    Nested_Dict['ConsolidationInformation']['Input'][a]['DocumentUUID'] = i
print(Nested_Dict)

Answered By: Eros
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.