def reduce_to_map(records, key_field, value_field):
 map_fields = {key_field, value_field}
 result_map = {}
 first_record = None
 for record in records:
  r = record.serialize()
  if (first_record is None):
   first_record = record
   first_record_items = set(r.items())
   continue
  diff = dict(first_record_items.difference(r.items()))
  different_keys = set(diff.keys()).difference(map_fields)
  if different_keys:
   raise ValueError('Unexpected related record found. \nReference Record: {}, \nDifferent Record: {}, \nDifferent Keys: {}'.format(first_record, record, different_keys))
  key = r[key_field]
  value = r[value_field]
  assert (key not in result_map)
  result_map[key] = value
 return result_map

def reduce_to_map ( records , key_field , value_field ) : map_fields = { key_field , value_field } result_map = { } first_record = None for record in records : r = record . serialize ( ) if ( first_record is None ) : first_record = record first_record_items = set ( r . items ( ) ) continue diff = dict ( first_record_items . difference ( r . items ( ) ) ) different_keys = set ( diff . keys ( ) ) . difference ( map_fields ) if different_keys : raise ValueError ( 'Unexpected related record found. \nReference Record: {}, \nDifferent Record: {}, \nDifferent Keys: {}' . format ( first_record , record , different_keys ) ) key = r [ key_field ] value = r [ value_field ] assert ( key not in result_map ) result_map [ key ] = value return result_map
reduce the records to a dict of key_field: value_field extracted from each record .