How to concatenate dictionaries to create a new ? Dna Center Cisco

First of all , thanks to read me !

I have an issue regarding my python script , I need to concatenate two dictionnaries payload_1 and payload_2 to payload

payload_1 = {'groupName': 'POOL_UNDERLAY-LAN-AUTOMATION',
 'groupOwner': 'DNAC',
 'type': 'generic',
 'siteId': 'ffd0d540-6fee-41d1-ba42-430001cf042f',
 'ipPools': [{'parentUuid': '0b20b02e-341a-4584-9f79-27ed32682efc',
 'dhcpServerIps': ['1.1.1.1',
 '2.2.2.2'],
 'dnsServerIps': ['8.8.8.8'],
 'ipPoolOwner': 'DNAC',
 'shared': 'true',
 'gateways': ['192.168.0.1'],
 'ipPoolCidr': '192.168.0/24'}]}


payload_2 = {'groupName': 'POOL_AP',
 'groupOwner': 'DNAC',
 'type': 'generic',
 'siteId': 'ffd0d540-6fee-41d1-ba42-430001cf042f',
 'ipPools': [{'parentUuid': '0b20b02e-341a-4584-9f79-27ed32682efc',
 'dhcpServerIps': ['1.1.1.1',
 '2.2.2.2'],
 'dnsServerIps': ['8.8.8.8'],
 'ipPoolOwner': 'DNAC',
 'shared': 'true',
 'gateways': ['192.168.1.1'],
 'ipPoolCidr': '192.168.1/24'}]}

payload_1 and payload_2 >> are dictionnaries , and need to stay like that.

>>> type(payload_1)
<class 'dict'>

>>> type(payload_2)
<class 'dict'>

Try : payload = {**payload_1, **payload_2}

But I have only the last payload_2

>>>print(payload)
{'groupName': 'POOL_AP',
 'groupOwner': 'DNAC',
 'type': 'generic',
 'siteId': 'ffd0d540-6fee-41d1-ba42-430001cf042f',
 'ipPools': [{'parentUuid': '0b20b02e-341a-4584-9f79-27ed32682efc',
 'dhcpServerIps': ['1.1.1.1',
 '2.2.2.2'],
 'dnsServerIps': ['8.8.8.8'],
 'ipPoolOwner': 'DNAC',
 'shared': 'true',
 'gateways': ['192.168.1.1'],
 'ipPoolCidr': '192.168.1/24'}]}

If fact , I’m waiting this result : payload_1,payload2

{'groupName': 'POOL_UNDERLAY-LAN-AUTOMATION',
 'groupOwner': 'DNAC',
 'type': 'generic',
 'siteId': 'ffd0d540-6fee-41d1-ba42-430001cf042f',
 'ipPools': [{'parentUuid': '0b20b02e-341a-4584-9f79-27ed32682efc',
 'dhcpServerIps': ['1.1.1.1',
 '2.2.2.2'],
 'dnsServerIps': ['8.8.8.8'],
 'ipPoolOwner': 'DNAC',
 'shared': 'true',
 'gateways': ['192.168.0.1'],
 'ipPoolCidr': '192.168.0/24'}]}
{'groupName': 'POOL_AP',
 'groupOwner': 'DNAC',
 'type': 'generic',
 'siteId': 'ffd0d540-6fee-41d1-ba42-430001cf042f',
 'ipPools': [{'parentUuid': '0b20b02e-341a-4584-9f79-27ed32682efc',
 'dhcpServerIps': ['1.1.1.1',
 '2.2.2.2'],
 'dnsServerIps': ['8.8.8.8'],
 'ipPoolOwner': 'DNAC',
 'shared': 'true',
 'gateways': ['192.168.1.1'],
 'ipPoolCidr': '192.168.1/24'}]}

How can I do that ?

I try to use command like this :

payload = json.dumps(payload_1) + json.dumps(payload_2)

It’s Works but payload is now a string !!

>>> type(payload)
<class 'str'>
>>>

It’s not good for me !

Could you help me please ?

Thanks a lot

A dictionary maps keys to values. If you merge the two dictionaries, the keys from the latter one override those from the former. This can be useful is you have new information to update the first dict with.

If you want to keep the groups distinct, you likely want to make a list out of them ([payload_1, payload_2]). It’s hard to say without seeing the use case though; what do you intend to do with the “merged” data structure?

1 Like

Hi Nicolas, and welcome!

Can you simplify your example to show the basic features please? We
really don’t care about the exact keys and values, except to illustrate
the requirements. Other people might have more time, but I don’t have
either the time or the care factor to carefully study complicated
examples to understand your requirements.

Make it easy for us to help you!

If you have two dicts:

d1 = {'a': 1, 'b': 1}
d2 = {'b': 2, 'c': 2}

and concatenate them, what result do you want:

{'a': 1, 'b': 1, 'c': 2}
{'a': 1, 'b': 2, 'c': 2}
{'a': 1, 'b': 3, 'c': 2}
{'a': 1, 'b': [1, 2], 'c': 2}

In other words, if there are duplicate keys, what happens to the values?

  • first dict wins;
  • second dict wins;
  • add the values;
  • put the values into a list and keep them both;
  • something else?

It will also help if you tell us your Python version. Starting in Python
3.9, we have the dict union operator:

but I don’t know if that will do what you want.