1

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

[
    {
        "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"
    }
]

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.

[
    {
        "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"
    }

]

I have tried this so far

    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'
Biplov
  • 1,136
  • 1
  • 20
  • 44

2 Answers2

2

try this:

my_list.sort(key=lambda my_dict: my_dict["num_gurus"], reverse=True)

what this does is basically two things:

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

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: try \ except

read more here: https://docs.python.org/2.7/tutorial/errors.html, remember better safe than sorry!

DrPrItay
  • 793
  • 6
  • 20
1

For storing the sorted list as new list, you can do it using sorted() as:

sorted(my_list, key=lambda x: x['num_gurus'], reverse=True)
# returns sorted list

where my_list is your list of dict objects.

Else, if you want to sort the content of original list, i.e my_list, then use list.sort() as:

my_list.sort(key=lambda x: x["num_gurus"], reverse=True)
# sorts the original list

Check document on: How to do Sorting in list

Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126