For example, i have a dictionary:
Edern 38
Pellam 34
Ban 32
Lionel 30
Geraint 30
Brangaine 28
Erec 28
Guiron 28
Fisher 28
Elyan 28
Segwarides 26
In this case output will be:
Edern 38
Pellam 34
Ban 32
Geraint 30
Lionel 30
Brangaine 28
Elyan 28
Erec 28
Fisher 28
Guiron 28
Segwarides 26
i want to sort keys with the same values in alphabetical order, but don't touch elements with different keys? How to realize this?
If you represent your name-number pairs as a list of two-item tuples, you can use
groupby
to clump similar-numbered items together, and sort each group internally without affecting group order.Result:
...And if you really need these items in dict form, you can make an OrderedDict out of them, like so: