Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Unique Elements From Objects
00:00
5 left

Unique Elements From Objects

EasyPython

Problem

LSEG Workspace may return repeated instrument records when results are combined from multiple market-data sources. Given a list of dictionary objects and the name of a field that uniquely identifies an object, return the unique objects in their original order, keeping only the first object for each identifier.

Formal Specification

Implement unique_objects(objects, key), where objects is a list of dictionaries and key is a string. Every dictionary contains the specified key, and its value is hashable. Return a new list containing the first dictionary encountered for each distinct key value. Do not modify the input list.

Objects with different values in non-key fields are still duplicates when their values for key match. The returned dictionaries should be the original objects, not copies.

Constraints

  • 0 <= len(objects) <= 100,000
  • Each object contains the specified key
  • Each key value is hashable
  • The input list must not be modified
  • The first object for each identifier must be retained

Function Signature

def unique_objects(objects, key):
Interviewer

Your question is Unique Elements From Objects. Start with the requirements in the Question tab.

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.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.