Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Merging Dictionaries in Python

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Merging Dictionaries in Python. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Macy's catalog pipelines may need to combine two dictionaries of product attributes, such as base details and updated merchandising values. Write a function that merges two dictionaries without modifying either input.

If both dictionaries contain the same key, the value from the second dictionary must override the value from the first. The result should be a new shallow dictionary containing every key from both inputs.

Formal Specification

Implement merge_catalog_attributes(first, second):

  • Input: Two Python dictionaries, first and second. Keys are strings, and values may be any Python value.
  • Output: A new dictionary containing the union of both sets of keys.
  • Duplicate keys use the value from second.
  • Neither input dictionary may be modified.

Constraints

  • 0 <= len(first), len(second) <= 10^4
  • Keys are strings
  • Values may be any Python value
  • Neither input dictionary may be modified
  • The merge is shallow

Function Signature

def merge_catalog_attributes(first, second):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output