Answers for "how to join 2 dictionaries in python"

1

python merge dictionaries

def merge_dicts(dict1, dict2):
        """Here's an example of a for-loop being used abusively."""
        return {**dict2, **{k: (v if not (k in dict2) else (v + dict2.get(k)) if isinstance(v, list) else merge_dicts(v, dict2.get(k))) if isinstance(v, dict) else v for k, v in dict1.items()}}
Posted by: Guest on June-17-2021
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

python concatenate dictionaries

# list_of_dictionaries contains a generic number of dictionaries
# having the same type of keys (str, int etc.) and type of values
global_dict = {}

for single_dict in list_of_dictionaries:
    global_dict.update(single_dict)
Posted by: Guest on April-14-2021

Code answers related to "how to join 2 dictionaries in python"

Python Answers by Framework

Browse Popular Code Answers by Language