question 493 Production Readiness Capstone · easy
Index the zone table, refusing repeats
The performance fix needs the zone table as a dictionary built once. A table with the same zone ID twice is a data error: silently keeping one of the two would put trips in the wrong borough. Build the index and refuse repeats.
Function
index_by(rows, key)Returnsa dictionary from each row's
row[key] to the row itselfRaises
ValueError mentioning the repeated value when two rows share a keyRulethe rows are not changed
Examples
callindex_by([{'zone_id': 132, 'borough': 'Queens'}, {'zone_id': 186, 'borough': 'Manhattan'}], 'zone_id')returns{132: {'zone_id': 132, 'borough': 'Queens'}, 186: {'zone_id': 186, 'borough': 'Manhattan'}}
callindex_by([], 'zone_id')returns{}
callindex_by([{'code': 'CRD', 'description': 'Credit card'}], 'code')returns{'CRD': {'code': 'CRD', 'description': 'Credit card'}}