Sorting list with dictionaries values(Maximum to Minimum)
<p>I have an array with loads of dictionaries in it. However I want to sort dictionaries in a way where I have maximum value to a specific key in a dictionary.
For example I have a list that looks like this</p>

<pre><code>[
    {
        "num_gurus": 40,
        "id": 119749,
        "code": null,
        "name": "ART 198P",
        "short_name": "ART 198P",
        "title": "Directed Group Study",
        "department_long": null,
        "full_name": "Directed Group Study",
        "department_short": "ART"
    },
    {
        "num_gurus": 3,
        "id": 119825,
        "code": null,
        "name": "ASAMST 198P",
        "short_name": "ASAMST 198P",
        "title": "Supervised Group Study",
        "department_long": null,
        "full_name": "Supervised Group Study",
        "department_short": "ASAMST"
    },
    {
        "num_gurus": 200,
        "id": 119904,
        "code": null,
        "name": "AST 636",
        "short_name": "AST 636",
        "title": "Baudelaire: Art Poetry Modernity",
        "department_long": null,
        "full_name": "Baudelaire: Art Poetry Modernity",
        "department_short": "AST"
    }
]
</code></pre>

<p>I want my output to sort my dictionaries where the value of a key attribute 'num_gurus' is maximum to minimum. Expected output would be.</p>

<pre><code>[
    {
        "num_gurus": 200,
        "id": 119904,
        "code": null,
        "name": "AST 636",
        "short_name": "AST 636",
        "title": "Baudelaire: Art Poetry Modernity",
        "department_long": null,
        "full_name": "Baudelaire: Art Poetry Modernity",
        "department_short": "AST"
    }
    {
        "num_gurus": 40,
        "id": 119749,
        "code": null,
        "name": "ART 198P",
        "short_name": "ART 198P",
        "title": "Directed Group Study",
        "department_long": null,
        "full_name": "Directed Group Study",
        "department_short": "ART"
    },
    {
        "num_gurus": 3,
        "id": 119825,
        "code": null,
        "name": "ASAMST 198P",
        "short_name": "ASAMST 198P",
        "title": "Supervised Group Study",
        "department_long": null,
        "full_name": "Supervised Group Study",
        "department_short": "ASAMST"
    }

]
</code></pre>

<p>I have tried this so far</p>

<pre><code>    for items in load_as_json:
            for key, val in sorted(items['num_gurus'].iteritems(), key=lambda (k,v): (v,k), reverse=True):
                print key,val

This throws me error and doesn't do what I actually want to.
This is the error I got.
  File "utils.py", line 61, in GetPopularCoursesBasedOnGurus
    for key, val in sorted(str(items['num_gurus']).iteritems(), key=lambda (k,v): (v,k)):
AttributeError: 'str' object has no attribute 'iteritems'
</code></pre>

<p>try this:</p>

<pre><code>my_list.sort(key=lambda my_dict: my_dict["num_gurus"], reverse=True)
</code></pre>

<p>what this does is basically two things:</p>

<ul>
<li>key paramater expects an anonymous function (lambda in python) and then sorts
the original list values by the values returned by
lambda function. <code>lambda my_dict: my_dict["num_gurus"]</code> returns the "num_gurus" item within each dictionary hence the list is sorted by those values.</li>
<li><code>reverse=True</code> by default sort function sorts from min to max, hence
this simply reverses that</li>
</ul>

<p>also I find this very "unsafe" as you have no guarentee for "num_gurus" key within your dictionaries, or a dictionary as a key value, hence I'd personally wrap this with some exception handler:  <code>try</code> \ <code>except</code></p>

<p>read more here: <a href="https://docs.python.org/2.7/tutorial/errors.html" rel="nofollow">https://docs.python.org/2.7/tutorial/errors.html</a>, remember better safe than sorry!</p>

python arrays sorting dictionary