0

I am trying to merge two dictionaries of dictionaries in aws lambda python 3 This does not work, I am using boto3

a = {'Action': 'DELETE', 'ResourceRecordSet': {'AliasTarget': {'HostedZoneId': 'BLABLABLA', 'EvaluateTargetHealth': False, 'DNSName': 'BLABLA'}, 'Type': 'A', 'Name': 'BLABLABLA'}}

b = {'Action': 'UPSERT', 'ResourceRecordSet': {'TTL': 60, 'Type': 'CNAME', 'Name': 'BLABLA', 'ResourceRecords': [{'Value': 'blablabla'}]}}

c = a_dictionary.update(b_dictionary)
print(c)

The print command prints none

Lucas
  • 6,129
  • 4
  • 24
  • 42
Yanir
  • 85
  • 7
  • What should the resulting dictionary be like for the key `Action`? – Celius Stingher May 09 '21 at 14:51
  • I want it to print {'Action': 'DELETE', 'ResourceRecordSet': {'AliasTarget': {'HostedZoneId': 'BLABLABLA', 'EvaluateTargetHealth': False, 'DNSName': 'BLABLA'}, 'Type': 'A', 'Name': 'BLABLABLA'} ,'Action': 'UPSERT', 'ResourceRecordSet': {'TTL': 60, 'Type': 'CNAME', 'Name': 'BLABLA', 'ResourceRecords': [{'Value': 'blablabla'}]}} – Yanir May 09 '21 at 14:54
  • No I have already tried it this merge 2 dictionaries ,I need to merge dictionaries with dictionaries within them. – Yanir May 09 '21 at 14:59

2 Answers2

1

The snippet as posted will raise a name error since the dictionaries are named a and b, not a_dictionary and b_dictionary.

That aside, a.update(b) updates a in place and returns None.

To merge two dictionaries into a new dict, you can splat the dicts:

c = {**a, **b}

If you're using an older version of Python that doesn't support the syntax, two dicts can be merged with

c = dict(a, **b)

However, this won't do deep merging, which may be what you'd like -- it will simply overwrite "top-level" entries from a with those from b.

AKX
  • 123,782
  • 12
  • 99
  • 138
0

The update function doesn't return new dictionary, it updates the first dictionary. So in your case you should write:

a.update(b)

And a will have updated data.

vurmux
  • 8,742
  • 3
  • 21
  • 41