How to reverse order of values but not keys in a python dictionnary?

Viewed 38

What is the best way to reverse order of values but not keys in a python dictionnary ?

dictionnary = {'a': 3, 'b': 2, 'c': 1}
inversed_values = dictionnary.reverseValues()
print(inversed_values)

This should print out {'a': 1, 'b': 2, 'c': 3}

1 Answers

You can convert values to list then reverse them then create dict like below:

>>> dct = {'a': 3, 'b': 2, 'c': 1}
>>> dict(zip(dct.keys(),list(dct.values())[::-1]))
{'a': 1, 'b': 2, 'c': 3}

Related