2

I am trying to sort a dictionary containing dictionaries. Here is that dictionary:

mydict = {
  'b': {'play': 2, 'scratch': 5, 'face': 8},
  'c': {'do': 4, 'chew': 6},
  'a': {'wash': 1, 'drink': 10, 'give': 20, 'eat': 30}
}

I want the following result after sorting:

{
  'a': {'eat': 30, 'give': 20, 'drink': 10, 'wash': 1},
  'b': {'face': 8, 'scratch': 5, 'play': 2},
  'c': {'chew': 6, 'do': 4}
}

I will appreciate if you tell me how to solve this issue.

1
  • 3
    Dictionaries are unordered. Commented Oct 6, 2017 at 4:03

1 Answer 1

3

Creating an ordered version of mydict

Let's start with your dictionary:

>>> mydict = {
...   'b': {'play': 2, 'scratch': 5, 'face': 8},
...   'c': {'do': 4, 'chew': 6},
...   'a': {'wash': 1, 'drink': 10, 'give': 20, 'eat': 30}
... }

Ordinary dictionaries are unordered. Ordered dictionaries, however are available from the collections module:

>>> from collections import OrderedDict

We can convert your dictionary to an ordered dictionary as follows:

>>> d = OrderedDict(sorted(mydict.items()))
>>> d
OrderedDict([('a', {'give': 20, 'drink': 10, 'eat': 30, 'wash': 1}), ('b', {'scratch': 5, 'play': 2, 'face': 8}), ('c', {'do': 4, 'chew': 6})])

As you can see above, d is ordered as we want. Alternatively, we can look at just the keys and verify they are in the order that we want:

>>> d.keys()
odict_keys(['a', 'b', 'c'])

In other ways, our ordered dictionary d behaves just like a regular dictionary:

>>> d['a']
{'give': 20, 'drink': 10, 'eat': 30, 'wash': 1}

Ordering mydict by key while ordering the dictionaries inside it by value in descending order

If we want the dictionaries inside mydict to be sorted in descending order of value, we use an OrderedDict again:

>>> mydict['a']
{'give': 20, 'drink': 10, 'eat': 30, 'wash': 1}
>>> OrderedDict(sorted(mydict['a'].items(), key=lambda v: -v[-1]))
OrderedDict([('eat', 30), ('give', 20), ('drink', 10), ('wash', 1)])

If we want to apply this ordering to all entries of mydict:

>>> d = OrderedDict( sorted( (key1, OrderedDict(sorted(value.items(), key=lambda v: -v[-1]))) for (key1, value) in mydict.items()) )
>>> d
OrderedDict([('a', OrderedDict([('eat', 30), ('give', 20), ('drink', 10), ('wash', 1)])), ('b', OrderedDict([('face', 8), ('scratch', 5), ('play', 2)])), ('c', OrderedDict([('chew', 6), ('do', 4)]))])
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much for your comment. I tried what you tell me. What I want to get the result is " {'eat': 30, 'give': 20, 'drink': 10, 'wash': 1}". The result is sorted by "value", not key. I'll be glad if you know how to manage.
@RyoheiDeIwata OK. I think I see what you want: I added a section at the end of the answer to show how to sort the inside dictionaries by value in descending order with mydict still ordered by key in ascending order of key.
Thank you so much! I make it because of you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.