-1

I have a dictionary inside a dictionary and I wish to print the whole dictionary but sorted around a value in the sub dictionary

Lesson = {Name:{'Rating':Rating, 'Desc':Desc, 'TimeLeftTask':Timeleft}}

or

Lesson = {'Math':{'Rating':11, 'Desc':'Exercises 14 and 19 page 157', 'TimeLeftTask':7}, 'English':{'Rating':23, 'Desc':'Exercise 5 page 204', 'TimeLeftTask':2}}

I want to print this dict for example but sorted by 'Rating' (high numbers at the top)
I have read this post but i don't fully understand it. If you could keep it simple it would be great.
And yes i'm making a program to sort and deal with my homework
Thanks in advance

Community
  • 1
  • 1
TheD0ubleT
  • 51
  • 1
  • 1
  • 11
  • The question you linked to answers your question. Have you tried applying it to your dict? What don't you understand? – dano Jun 19 '14 at 16:47
  • 1
    possible duplicate of [Sort dictionary of dictionaries by value](http://stackoverflow.com/questions/13573123/sort-dictionary-of-dictionaries-by-value) – dano Jun 19 '14 at 16:49
  • The problem is, that it gives me a syntax error and, because i don't unerstand what i'm doing, i can't fix it – TheD0ubleT Jun 19 '14 at 17:16

1 Answers1

0
def sort_by_subdict(dictionary, subdict_key):
    return sorted(dictionary.items(), key=lambda k_v: k_v[1][subdict_key])

Lesson = {'Math':{'Rating':11, 'Desc':'Exercises 14 and 19 page 157', 'TimeLeftTask':7}, 'English':{'Rating':23, 'Desc':'Exercise 5 page 204', 'TimeLeftTask':2}}
print(sort_by_subdict(Lesson, 'Rating'))

As there is no notion of order in dictionary, we need to represent the dictionary as a list of key, value pair tuples to preserve the sorted order.

The so question you mention sorts the dictionary using the sorted function such that it returns a list of (k, v) tuples (here k means key & v means value) of top level dictionary, sorting by the desired value of sub dictionary v.

Community
  • 1
  • 1
Parth
  • 729
  • 8
  • 23
  • When i do that i get invalid syntax. I'm on python 3.4 btw the error is on sorted(dictionary.iteritems(), key=lambda (k, v): v[key_in_values]), more specificaly on key=lambda here -->(<-- k, v) on that parenthesis – TheD0ubleT Jun 19 '14 at 17:31
  • @TheD0ubleT You have to do it like this in Python 3: `key=lambda k_v: k_v[1][key_in_values]` – dano Jun 19 '14 at 17:37
  • I'm getting this line 148, in return sorted(dictionary.items(), key=lambda k_v: k_v[1][key_in_values]) NameError: name 'key_in_values' is not defined – TheD0ubleT Jun 19 '14 at 20:49
  • @TheD0ubleT, I've ported the code in my answer to python3 & is tested on python 3.4. Moreover, the NameError must have been thrown because the name `key_in_values` is changed to `subdict_key`. :-) – Parth Jun 20 '14 at 00:41
  • Last thing, it's arranged from lowest to highest (ascending order), i'd like it to be in decending order, is it possible? – TheD0ubleT Jun 20 '14 at 05:47
  • Just pass an additional argument `reverse=True` to the function `sorted`. – Parth Jun 20 '14 at 12:29