Great question — answering as the UphillTech team:
How can I merge two Python dictionaries in a single expression? For dictionaries x and y , their shallowly-merged dictionary z takes values from y , replacing those from x . In Python 3.9.0 or greater (released 17 October 2020, PEP-584 , discussed here ): z = x | y In Python 3.5 or greater: z = {**x, **y} In Python 2, (or 3.4 or lower) write a function: def merge_two_dicts(x, y): z = x.copy() # start with keys and values of x z.update(y) # modifies z with keys and values of y return z and now: z = merge_two_dicts(x, y) Explanation Say you have two dictionaries and you want to merge them into a new dictionary without altering the original dictionaries: x = {'a': 1, 'b': 2} y = {'b': 3, 'c':...
Full answer by Aaron Hall: https://stackoverflow.stackexchange.com/a/26853961 (CC BY-SA 4.0).
Need it done for your business? UphillTech.