Question: I want to reverse dictionary `key, value` pairs using a dictionary comprehension, but if the new dictionary has more than one value for a key then it is getting replaced with the last value.

Is it possible to append to the values in the new dictionary if a key is repeated, using a comprehension?

Input:

```
test_di = {'a':'1', 'b':'2', 'c':'3', 'd':'2'}

```

Code:

```
{v:k for k,v in test_di.items()} 

```

Output of this code:

```
{'1': 'a', '3': 'c', '2': 'd'}

```

Desired output:

```
{'1': ['a'], '3': ['c'], '2': ['b','d']}

```

Answer: 
It's not possible to do it in a reasonable way (i.e. `O(N)` time) with a dictionary comprehension. The comprehension simply can't handle duplicated values.

However, it's quite easy with a regular loop:

```
d = {}
for key, value in old_d.items():
    d.setdefault(value, []).append(key)

```
A `defaultdict` would be the most efficient approach:

```
from collections import defaultdict

test_di = {'a':'1', 'b':'2', 'c':'3', 'd':'2'}

d = defaultdict(list)
for v,k in test_di.items():
    d[k].append(v)

print(d)

```