runtime
493 / 500
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.

Functionindex_by(rows, key)
Returnsa dictionary from each row's row[key] to the row itself
RaisesValueError mentioning the repeated value when two rows share a key
Rulethe 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'}}

Loading your progress…

Index the zone table, refusing repeats — Python practice · Runtime