accumulate values by key
Input
[('a', 1), ('b', 2), ('a', 3)]
Output
{'a': [1, 3], 'b': [2]}
Values for 'a' are collected in the list [1, 3] in the order encountered.
Full lesson preview
Turn a list of (key, value) pairs into a dictionary mapping keys to lists of their values.
Problem statement
Task
Examples
Input
[('a', 1), ('b', 2), ('a', 3)]
Output
{'a': [1, 3], 'b': [2]}
Values for 'a' are collected in the list [1, 3] in the order encountered.
Input format
Output format
Constraints
Samples
Input
[(1, 'one'), (2, 'two'), (1, 'uno')]
Output
{1: ['one', 'uno'], 2: ['two']}
Integer keys are preserved; lists keep insertion order.