Iterate and Transform Items

Easy ⏱ 8 min 78% acceptance ★★★★★ 4.8
Write a function apply_discount(prices, pct) that takes a dict mapping product names to prices and a discount percentage pct (e.g. 10 for 10%). Return a new dict with the same keys, each value reduced by pct percent and rounded to 2 decimal places.

Examples

Example 1
Input
apply_discount({'pen': 100.0, 'bag': 50.0}, 10)
Output
{'pen': 90.0, 'bag': 45.0}
Explanation

Each price reduced by 10%.

Constraints

  • Do not mutate the input dict.
  • Round each result to 2 decimals.

Topics

Dictionariesiterating items()

Companies

AccentureCognizant

Hints

Hint 1

Iterate with `.items()`, build a new dict: `{k: round(v * (1 - pct / 100), 2) for k, v in prices.items()}`.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.