What a Drop Costs

Data with PythonEasy+10 XP+10

What a Drop Costs

Before throwing rows away, it's worth knowing how many you'd keep.

Write rows_kept(replies, needed). replies is a list of dictionaries (one per row, with None for a blank box). needed is a list of the column names your analysis actually uses.

Return a dictionary with two whole numbers:

  • "drop_any": how many rows are left if you drop every row with a blank anywhere
  • "drop_needed": how many rows are left if you only drop rows with a blank in one of the needed columns

replies always has at least one row, and needed always names at least one column.

Examples

replies = [{"name": "Ana", "age": 34, "city": "Leeds"}, {"name": "Ben", "age": None, "city": "York"}, {"name": "Cara", "age": 29, "city": None}, {"name": "Dev", "age": 41, "city": "Bath"}, {"name": "Eli", "age": None, "city": "York"}]
needed = ["age"]
returns {'drop_any': 2, 'drop_needed': 3}
replies = [{"item": "tea", "price": 2.5, "note": None}, {"item": "cake", "price": 3.0, "note": None}, {"item": "scone", "price": None, "note": "stale"}]
needed = ["item", "price"]
returns {'drop_any': 0, 'drop_needed': 2}

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run