M10) Copying dictionary records within a threshold
A table is stored as a list of dictionaries. The first dictionary contains the column labels; every later dictionary contains an experiment ID and a numeric KPI (a measured performance value). copy_below_threshold(t, table) must return a new table containing a copied header followed by a copy of every data record whose KPI is less than or equal to t. It must preserve input order, record structure, and the input table.
The example in Listing 1 has threshold 0.7. Its required output contains the header and experiment IDs 1, 3, and 5. record.copy() creates a new dictionary with the same keys and values.
Compare the four implementations in Listing 2.
Which implementation satisfies the stated requirements?
TipHint
record.copy()returns a new dictionary with the same keys and values. Forrecord = {"id": 8, "kpi": 0.4}, its copy is{"id": 8, "kpi": 0.4}. The records here contain only numbers or strings.record.items()visits(key, value)pairs: a record{"id": 8}yields the pair("id", 8).result.append(item)adds one item to the end of the list and returnsNone.- A slice
table[1:]selects every item from position1onward without changingtable.