Back to Practice
#0093
Invert Dictionary to List Values
MediumPython25 min20 XP
Problem
Given a dictionary, return a new dictionary where each original value maps to a sorted list of keys that had that value.
Why This Matters
Inverting mappings is common in indexing, grouping, lookup table cleanup, and feature engineering.
Function Signature
def invert_to_groups(mapping):
Examples
Example 1
Input{"a": 1, "b": 2, "c": 1}
Output{1: ["a", "c"], 2: ["b"]}
Keys a and c shared value 1, so they are grouped together.
Constraints
- Return the exact structure requested.
- Handle the edge cases mentioned in the prompt.
- Prefer clear logic over clever one-liners.
CodePython
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Core example
Input{"a": 1, "b": 2, "c": 1}
Expected{1: ["a", "c"], 2: ["b"]}
Keys a and c shared value 1, so they are grouped together.
Hidden Test Categories
empty dictall same valueall unique values