Python Forum
nested dictionary - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: nested dictionary (/thread-16772.html)



nested dictionary - Maxime - Mar-13-2019

Hello.
I have a nested dictionary that I want to sort.

dictionary={'Hello': {'D1': 1, 'D3': 2}, 
            'I': {'D1': 2, 'D2': 2, 'D3': 3}, 
            'Am': {'D1': 2}, 
            'A': {'D1': 1, 'D2': 1}, 
            'Student': {'D1': 1}, 
            'This': {'D1': 1}, 
            'Is': {'D1': 3, 'D2': 1}, 
            'My': {'D1': 1}, 
            'Homework': {'D1': 1})}
I want to sort it using the values of D1, D2, D3 in each nested dictionary.
I want the result to be like this:

sorted_dictionary={'Hello': {'D3': 2, 'D1': 1}, 
                  'I': {'D3': 3, 'D1': 2, 'D2': 2}, 
                  'Am': {'D1': 2}, 
                  'A': {'D1': 1, 'D2': 1}, 
                  'Student': {'D1': 1}, 
                  'This': {'D1': 1}, 
                  'Is': {'D1': 3, 'D2': 1}, 
                  'My': {'D1': 1}, 
                  'Homework': {'D1': 1})}



RE: nested dictionary - ichabod801 - Mar-13-2019

Dictionaries don't sort. The best you could do is use collections.OrderedDict. However, that doesn't sort either, it just remembers the order items were inserted. You you would need to extract the items to a list, so the list, and then enter them in order into the OrderedDict.


RE: nested dictionary - Maxime - Mar-13-2019

Got it. I already extracted nested dictionaries as list and sorted them. But I thought I could sort directly in the dictionary. Anyway, thank a lot


RE: nested dictionary - perfringo - Mar-14-2019

Ichabod801 said that dictionaries don't sort. He is right. However, there are some details to know about sorted() function:

Quote:Return a new sorted list from the items in iterable.

Two important points - you always get new object which is always list; you can sort whatever iterable (including sets which have no order and tuples which are immutable, dictionaries which are mappings and not sequences but support iteration).

While you can't sort dictionary in-place you can get new dictionary which is sorted (Python >=3.6):

>>> dictionary={'Hello': {'D1': 1, 'D3': 2}, 
...             'I': {'D1': 2, 'D2': 2, 'D3': 3}, 
...             'Am': {'D1': 2}, 
...             'A': {'D1': 1, 'D2': 1}, 
...             'Student': {'D1': 1}, 
...             'This': {'D1': 1}, 
...             'Is': {'D1': 3, 'D2': 1}, 
...             'My': {'D1': 1}, 
...             'Homework': {'D1': 1})}
>>> {k: dict(sorted(v.items(), key=lambda x: x[1], reverse=True)) for k, v in dictionary.items()}
{'Hello': {'D3': 2, 'D1': 1},
 'I': {'D3': 3, 'D1': 2, 'D2': 2},
 'Am': {'D1': 2},
 'A': {'D1': 1, 'D2': 1},
 'Student': {'D1': 1},
 'This': {'D1': 1},
 'Is': {'D1': 3, 'D2': 1},
 'My': {'D1': 1},
 'Homework': {'D1': 1}}
While it's OK as homework in real life scenarios I recommend think twice before relying on dictonary order.