I have a python list which contains dictionaries and I want to make a new list which contain dictionaries with unique keys and associated list values like below:
Input:
[{1: 2}, {2: 2}, {1: 3}, {2: 1}, {1: 3}]
Output:
[{1:[2,3,3]},{2:[2,1]}]
Thanks in advance.
How about:
Result:
[update]
First the
((x, y),) = d.items():input, like{1: 2}d.items()will be something analogous to[(1, 2)],(otherwise the interpreter will think the outer parenthesis are doing grouping instead of defining a single element tuple)The
r.setdefault(x, []).append(y)is analogous to: