Simple inversion with duplicates
Input
invert_dict({'a': 1, 'b': 2, 'c': 1})
Output
{1: ['a', 'c'], 2: ['b']}
Value 1 appears for 'a' then 'c', so inverted[1] is ['a', 'c']; 2 maps to ['b'].
Full lesson preview
Create the inverse of a mapping: values become keys and keys become values (as lists) to handle duplicate values.
Problem statement
Task
Examples
Input
invert_dict({'a': 1, 'b': 2, 'c': 1})
Output
{1: ['a', 'c'], 2: ['b']}
Value 1 appears for 'a' then 'c', so inverted[1] is ['a', 'c']; 2 maps to ['b'].
Input format
Output format
Constraints
Samples
Input
{'x': 'apple', 'y': 'banana', 'z': 'apple'}
Output
{'apple': ['x', 'z'], 'banana': ['y']}
Keys 'x' and 'z' both had value 'apple', so they are collected under 'apple'.