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: 
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)

```
You can use an `OrderedDict` and reverse the keys. A normal `dict` does not keep track of the order of the keys.

```
from collections import OrderedDict

dict1 = OrderedDict([('Name', 'Jon'), ('Race', 'Latino'), ('Job', 'Nurse')])
reversed_dict1 = OrderedDict([(key, dict1.get(key)) for key in reversed(dict1)])

```