Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sort a list of dictionaries which contains list's dicts value

I am building a simple app and I am trying to get json response from api But it doesn’t have a sort method so I am trying to sort it after fetch in python function, But the problem is that, It is not sorting the list dict.

function.py

json_data = [
    {
        "instanceId": "19",
        "instanceData": [
            {
                "blogLikes": "166647",
                "blogComments": "7713",
            }
        ]
    },
    {
        "instanceId": "3",
        "instanceData": [
            {
                "blogLikes": "236655",
                "blogComments": "8304",
            }
        ]
    },
    {
        "instanceId": "17",
        "instanceData": [
            {
                "blogLikes": "246865",
                "blogComments": "7213",
            }
        ]
    }
]



def sort_dict(json_data):
    new_dict = sorted(json_data, key=lambda k: k[-1]['instanceData'][0]['blogLikes'])
    print(new_dict)

    return JsonResponse({"example": "nothing"})

When I try to run the above function but it is showing

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

KeyError at /sort_dict/
-1

I have tried many times by different method like :-

newlist = sorted(json_data, key=lambda d: d['instanceData'])

then it showed me

‘<‘ not supported between instances of ‘dict’ and ‘dict’

Then I tried

newlist = sorted(json_data, key=lambda d: list(d.keys()))

It didn’t raise any error But it returns unexpected result not sorted list.

What I am trying to do ?

I am trying to sort above list’s dict’s according to blogLikes

like :-

json_data = [
    {
        "instanceId": "17",
        "instanceData": [
            {
                "blogLikes": "246865",
                "blogComments": "7213",
            }
        ]
    },
    {
        "instanceId": "3",
        "instanceData": [
            {
                "blogLikes": "236655",
                "blogComments": "8304",
            }
        ]
    },
    {
        "instanceId": "19",
        "instanceData": [
            {
                "blogLikes": "166647",
                "blogComments": "7713",
            }
        ]
    }
]

Any help would be much appreciated.

>Solution :

What is the purpose of k[-1]? Also, if you want it sorted in descending order you need reverse=True:

json_data = [
    {
        "instanceId": "19",
        "instanceData": [
            {
                "blogLikes": "166647",
                "blogComments": "7713",
            }
        ]
    },
    {
        "instanceId": "3",
        "instanceData": [
            {
                "blogLikes": "236655",
                "blogComments": "8304",
            }
        ]
    },
    {
        "instanceId": "17",
        "instanceData": [
            {
                "blogLikes": "246865",
                "blogComments": "7213",
            }
        ]
    }
]



def sort_dict(json_data):
    new_dict = sorted(json_data, key=lambda k: k['instanceData'][0]['blogLikes'], reverse=True)
    print(new_dict)

sort_dict(json_data)

output

[{'instanceId': '17', 'instanceData': [{'blogLikes': '246865', 'blogComments': '7213'}]}, {'instanceId': '3', 'instanceData': [{'blogLikes': '236655', 'blogComments': '8304'}]}, {'instanceId': '19', 'instanceData': [{'blogLikes': '166647', 'blogComments': '7713'}]}]

Note, I keep it as close to your code as possible – e.g. I would return from the function instead of print inside it.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading