Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
nested dictionary
#1
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})}
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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
Reply
#4
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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Looping to Create Nested Dictionary gngu2691 10 33,535 Jun-22-2018, 04:11 PM
Last Post: anickone

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020