2

I have the following dictionary.

var = a = { 
  'Black': { 'grams': 1906, 'price': 2.05},
  'Blue': { 'grams': 9526, 'price': 22.88},
  'Gold': { 'grams': 194, 'price': 8.24},
  'Magenta': { 'grams': 6035, 'price': 56.69},
  'Maroon': { 'grams': 922, 'price': 18.76},
  'Mint green': { 'grams': 9961, 'price': 63.89},
  'Orchid': { 'grams': 4970, 'price': 10.78},
  'Tan': { 'grams': 6738, 'price': 50.54},
  'Yellow': { 'grams': 6045, 'price': 54.19}
}

How can I sort it based on the price. So the resulting dictionary will look like below.

result = { 
  'Black': { 'grams': 1906, 'price': 2.05},
  'Gold': { 'grams': 194, 'price': 8.24},
  'Orchid': { 'grams': 4970, 'price': 10.78},
  'Maroon': { 'grams': 922, 'price': 18.76},
  'Blue': { 'grams': 9526, 'price': 22.88},
  'Tan': { 'grams': 6738, 'price': 50.54},
  'Magenta': { 'grams': 6035, 'price': 56.69},
  'Mint green': { 'grams': 9961, 'price': 63.89}, 
}
Pattu
  • 3,481
  • 8
  • 32
  • 41
  • 2
    possible duplicate to http://stackoverflow.com/questions/4110665/sort-nested-dictionary-by-value-and-remainder-by-another-value-in-python – Deca Jun 29 '16 at 09:19

4 Answers4

12

Construct an OrderedDict from a list of ordered item tuples:

from collections import OrderedDict

ordered = OrderedDict(sorted(a.items(), key=lambda i: i[1]['price']))

(.items() assumes Python 3, in Python 2 iteritems should do the same.)

deceze
  • 510,633
  • 85
  • 743
  • 889
2

You can also use getitem from the operator library:

from collections import OrderedDict
from operator import getitem

sorted_dict = OrderedDict(sorted(a.items(), key = lambda x:getitem(x[1],'price')))

print(sorted_dict)

Output:

OrderedDict([('Black', {'grams': 1906, 'price': 2.05}), ('Gold', {'grams': 194, 'price': 8.24}), ('Orchid', {'grams': 4970, 'price': 10.78}), ('Maroon', {'grams': 922, 'price': 18.76}), ('Blue', {'grams': 9526, 'price': 22.88}), ('Tan', {'grams': 6738, 'price': 50.54}), ('Yellow', {'grams': 6045, 'price': 54.19}), ('Magenta', {'grams': 6035, 'price': 56.69}), ('Mint green', {'grams': 9961, 'price': 63.89})])
RoadRunner
  • 25,803
  • 6
  • 42
  • 75
1
for s in sorted(a.iteritems(), key=lambda (x, y): y['price']):
       print s

Or by OrderedDict

from collections import OrderedDict
res = OrderedDict(sorted(a.items(), key=lambda x: x[1]['price'], reverse=False))
print res

Output:

[('Black', {'price': 2.05, 'grams': 1906}), ('Gold', {'price': 8.24, 'grams': 194}), ('Orchid', {'price': 10.78, 'grams': 4970}), ('Maroon', {'price': 18.76, 'grams': 922}), ('Blue', {'price': 22.88, 'grams': 9526}), ('Tan', {'price': 50.54, 'grams': 6738}), ('Yellow', {'price': 54.19, 'grams': 6045}), ('Magenta', {'price': 56.69, 'grams': 6035}), ('Mint green', {'price': 63.89, 'grams': 9961})]
khelili miliana
  • 3,730
  • 2
  • 15
  • 28
0
import collections

update=collections.OrderedDict()
result = sorted(a, key=lambda x: (a[x]['price']))
for r in result:
    update[r]=a[r]

print(update)
onkar
  • 4,427
  • 10
  • 52
  • 89
  • It worked. But I was thinking there must be another way to combine the for loop with sorted operation to get result. – Pattu Jun 29 '16 at 09:35
  • Since you need them sorted, we need to make use of OrderedDict.For that case,IMO for loop is required. – onkar Jun 29 '16 at 09:46