I have the following object:
{
a: {size: 4},
b: {size: 8},
c: {size: 2},
d: {size: 3},
e: {size: 9},
f: {size: 23}
}
I need to sort this based on size in Python 2. I can find a lot of resource on shallow sorting, but I am not sure how to access the size value. I struggled with sorted and a lamba function, but it isn't working out. How do I go about this?
I last attempted this:
domains = sorted(items, key=lambda item: item['size'])
However got the error:
TypeError: string indices must be integers, not str
Edit
I used @smarx's example to fix. As I needed to rebuild the full object and not just the keys, I ended up using:
for name in sorted(items, key=lambda k: items[k]['size'], reverse=True):
lambdacorrectly (assuming you made same typo in code).size... but you don't explicitly say that ... be more explicit as to your problemdict, not thedict's items. The error is what you'd see if you tried to index the key (str).