Sam Loxton wrote: > I am fairly new to the python language and am trying to sort a nested > Dictionary of a Dictionary which I wish to sort by value. The dictionary > does not have to be restructured as I only need it sorted in this way > for printing purposes. > > The following is an example of my Dictionary printed with 'print > dictionary.items()', where '2329513' is the key of the first hash, 'ops' > is the key of the second hash and '50.0' is the value of the second hash > which I would like to sort by: > [('2329513', {'ops': 20.0}), ('2329492', {'ops': '80'}), ('2329490', > {'ops': '50'})] > > I hope to sort these first keys by the value of the 'ops' key from > highest to lowest value to give the following result: > [('2329492', {'ops': '80'}), ('2329490', {'ops': '50'}), ('2329513', > {'ops': 20.0})]
If Dennis' remarks don't apply because you simplified your problem for the post: >>> d = {'2329513': {'ops': 20.0}, '2329492': {'ops': '80'}, '2329490': {'ops': '50'}} >>> items = d.items() >>> def key(item): ... return item[1]["ops"] ... >>> items.sort(key=key, reverse=True) >>> items [('2329492', {'ops': '80'}), ('2329490', {'ops': '50'}), ('2329513', {'ops': 20.0})] Peter -- http://mail.python.org/mailman/listinfo/python-list