0

I want merge all elements of results

data in results:

element 0:

{'00000001': {'foo_id': 83959370, 'bar_id': 'ABCD1'}, '00000002': {'foo_id': 83959371, 'bar_id': 'ABCD2'}}

element 1:

{'00000003': {'foo_id': 83959372, 'bar_id': 'ABCD2'}, '00000004': {'foo_id': 83959373, 'bar_id': 'ABCD4'}}

element 2:

{'00000005': {'foo_id': 83959374, 'bar_id': 'ABCD3'}, '00000006': {'foo_id': 83959375, 'bar_id': 'ABCD6'}}

with ThreadPoolExecutor(max_workers=len(foo_ids_chunks)) as executor:
        # Use the map method to apply the send_request function to each chunk in parallel
        results = executor.map(send_request, foo_ids_chunks, [authorisation_token] * len(foo_ids_chunks))
        logger.info(f"results {results}")
        # Merge the results into a single list
        for r in results:
            logger.info(f"result {r}")
        

I try 2 solutions but do not work:

merged_results = [item for sublist in results for item in sublist]

merged_results = {key: value for json_dict in results for key, value in json_dict.items()}

wanted result:

{
  "00000001":{
    "foo_id":83959370,
    "bar_id":"ABCD1"
  },
  "00000002":{
    "foo_id":83959371,
    "bar_id":"ABCD2"
  },
  "00000003":{
    "foo_id":83959372,
    "bar_id":"ABCD2"
  },
  "00000004":{
    "foo_id":83959373,
    "bar_id":"ABCD4"
  },
  "00000005":{
    "foo_id":83959374,
    "bar_id":"ABCD3"
  },
  "00000006":{
    "foo_id":83959375,
    "bar_id":"ABCD6"
  }
}
Stéphane GRILLON
  • 11,140
  • 10
  • 85
  • 154
  • please add expected merge result, normally to do json merge update method can be used – Raushan Kumar Dec 07 '22 at 10:40
  • @RaushanKumar, I add wanted result in post – Stéphane GRILLON Dec 07 '22 at 10:45
  • above one doesn't look valid json for me. is that you want like this. {"00000001": {"foo_id": 83959370, "bar_id": "ABCD1"}, "00000002": {"foo_id": 83959371, "bar_id": "ABCD2"}, "00000003": {"foo_id": 83959372, "bar_id": "ABCD2"}, "00000004": {"foo_id": 83959373, "bar_id": "ABCD4"}, "00000005": {"foo_id": 83959374, "bar_id": "ABCD3"}, "00000006": {"foo_id": 83959375, "bar_id": "ABCD6"}} – Raushan Kumar Dec 07 '22 at 10:53

1 Answers1

1

Did you mean something like this,

merged_results = {}
for element in results:
    merged_results.update(element)

Output: {'00000001': {'foo_id': 83959370, 'bar_id': 'ABCD1'}, '00000002': {'foo_id': 83959371, 'bar_id': 'ABCD2'}, '00000003': {'foo_id': 83959372, 'bar_id': 'ABCD2'}, '00000004': {'foo_id': 83959373, 'bar_id': 'ABCD4'}, '00000005': {'foo_id': 83959374, 'bar_id': 'ABCD3'}, '00000006': {'foo_id': 83959375, 'bar_id': 'ABCD6'}}

chipsy
  • 65
  • 6