Answers for "merge 2 dicts python"

4

python join dict

dict.update([other])
Posted by: Guest on April-02-2020
1

python join dict

def mergeDict(dict1, dict2):
   ''' Merge dictionaries and keep values of common keys in list'''
   dict3 = {**dict1, **dict2}
   for key, value in dict3.items():
       if key in dict1 and key in dict2:
               dict3[key] = [value , dict1[key]]
 
   return dict3
 
# Merge dictionaries and add values of common keys in a list
dict3 = mergeDict(dict1, dict2)
 
print('Dictionary 3 :')
print(dict3)
Posted by: Guest on April-02-2020
0

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}}
Posted by: Guest on April-07-2020
-2

concat dicts python

d1={1:2,3:4}; d2={5:6,7:9}; d3={10:8,13:22}
d4 = dict(d1, **d2); d4.update(d3)
Posted by: Guest on July-10-2020

Python Answers by Framework

Browse Popular Code Answers by Language