A Year in One Table

Data with PythonMedium+10 XP+10

A Year in One Table

A bakery's till exports one table per month. Each table has a city and an amount column. From some month on, the till also records a staff column, which this report doesn't need.

Task: write city_year(monthly_sales).

  • monthly_sales is a list of tables, one per month. Each table is a dictionary of lists with at least the keys "city" and "amount". Some tables also have "staff". Every table has at least one row.
  • Return a dictionary from each city to a list [total, orders]: the city's total amount across all months as an int, and how many rows (orders) it had across all months, also an int.

Examples

monthly_sales = [{'city': ['Leeds', 'York'], 'amount': [12, 8]}, {'city': ['Bath', 'Leeds'], 'amount': [6, 15]}]
returns {'Bath': [6, 1], 'Leeds': [27, 2], 'York': [8, 1]}
monthly_sales = [{'city': ['York'], 'amount': [9]}, {'city': ['York', 'Hull'], 'amount': [4, 7], 'staff': [2, 3]}, {'city': ['Hull'], 'amount': [5], 'staff': [1]}]
returns {'Hull': [12, 2], 'York': [13, 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