PY-36

Join Record Collections by ID

  • Medium
  • Records and Alignment
  • Python

Task

Write join_records_by_id(left, right) for two lists of record dictionaries. Every record has an id key. Return a three-item tuple (pairs, status, details) and do not modify either input list or any record.

Validate the inputs in this order:

  1. If left repeats an ID, return ([], "duplicate left ids", duplicates). duplicates is a list of each repeated ID at its first repeated occurrence, in left source order.
  2. Otherwise, if right repeats an ID, return ([], "duplicate right ids", duplicates) using right source order.
  3. Otherwise, if IDs from left are absent from right, return ([], "missing right ids", ids), with IDs in left source order.
  4. Otherwise, if IDs from right are absent from left, return ([], "extra right ids", ids), with IDs in right source order.

If none of those conditions applies, return (pairs, "ok", []). pairs is a list of (left_record, right_record) tuples in left source order. Records are matched by equal IDs. Stop at the first applicable condition; for example, duplicate left IDs take precedence over every right-side condition.

Example

The record dictionaries themselves are returned in the pairs; make no copied or modified versions of them.

Your implementation

Edit solution.py and keep this function name and signature:

IDs are hashable. A duplicate is reported only once, when its second occurrence is encountered. Return the exact status strings and list forms described above.