Home > Software engineering >  Sort a list of dictionaries which contains list's dicts value
Sort a list of dictionaries which contains list's dicts value

Time:06-12

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

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.

CodePudding user response:

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.

  • Related