I have a Python dictionary d
:
d = {
'a': 1,
'b': 2,
'c': 3,
'd': 4,
}
I want to sort this such that:
- The key with value
x
is first. - Everything else is sorted alphabetically.
So if x = 'c'
then I want the output to be:
>>> print(new_d)
>>> { 'c': 3, 'a': 1, 'b': 2, 'd': 4}
Is there a straightforward / easy / Pythonic way to do this? And yes I have seen and understand the plethora of answers for the questions How do I sort a dictionary by value? and How can I sort a dictionary by key?