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

Time:11-17

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}

CodePudding user response:

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