how to concatenate dictionaries in python code example

Example 1: merge two dict python 3

z = {**x, **y}

Example 2: python merge dictionaries

dict1 = {'color': 'blue', 'shape': 'square'}
dict2 = {'color': 'red', 'edges': 4}

dict1.update(dict2) #if a key exists in both, it takes the value of the second dict
# dict1 = {'color': 'red', 'shape': 'square', 'edges': 4}
# dict2 is left unchanged

Example 3: merge two list of dictionaries python with string

import pandas as pd

l1 = [{'id': 9, 'av': 4}, {'id': 10, 'av': 0}, {'id': 8, 'av': 0}]
l2 = [{'id': 9, 'nv': 45}, {'id': 10, 'nv': 0}, {'id': 8, 'nv': 30}]

df1 = pd.DataFrame(l1).set_index('id')
df2 = pd.DataFrame(l2).set_index('id')
df = df1.merge(df2, left_index=True, right_index=True)
df.T.to_dict()
# {9: {'av': 4, 'nv': 45}, 10: {'av': 0, 'nv': 0}, 8: {'av': 0, 'nv': 30}}