Return to site

Merge dictionaries python

broken image
broken image

Thus for key in dict2 loops over every key in dict2. Recall that iterating over a dictionary will produce each of its keys one-by-one. Let’s first see what this function does right.

broken image

Def buggy_merge_max_mappings ( dict1, dict2 ): # create the output dictionary, which contains all # the mappings from `dict1` merged = dict1 # populate `merged` with the mappings in dict2 if: # - the key doesn't exist in `merged` # - the value in dict2 is larger for key in dict2 : if key not in merged or dict2 > merged : merged = dict2 return merged

broken image