runtime
147 / 500
question 147 Advanced Functions & Recursion · medium

Fill in the missing fields

Product records arrive from suppliers with fields left out or set to None. Before import, each record is completed with the shop's default values, which differ from call to call. Write fill_missing(record, **defaults): return a new dictionary with every field of record, plus each default for a field that is absent or None. A field that has a real value keeps it, even when that value is 0 or "".

Functionfill_missing(record, **defaults)
recorda dictionary of field names to values
defaultsany number of named arguments, such as colour="black"
Returnsa new dictionary; record itself is left unchanged
Examples
callfill_missing({'sku': 'SPO-11681', 'colour': None, 'stock': 0}, colour='black', stock=10, material='cotton')returns{'sku': 'SPO-11681', 'colour': 'black', 'stock': 0, 'material': 'cotton'}
callfill_missing({'sku': 'HOM-22847', 'size': 'L'}, colour='white')returns{'sku': 'HOM-22847', 'size': 'L', 'colour': 'white'}
callfill_missing({'sku': 'ELE-24781', 'warranty': None}, warranty=12)returns{'sku': 'ELE-24781', 'warranty': 12}

Loading your progress…

Fill in the missing fields — Python practice · Runtime