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 nested list of dict by it's dict value

I have the following structure:

d = {
    'futures': {
        'test': {
            'nested': {
                1: {
                    'list': [
                             {
                                 'c': 'third',
                                 'price': 3
                             },
                             {
                                 'b': 'second',
                                 'price': 2
                             },
                             {
                                 'a': 'first',
                                 'price': 1
                             }                             
                    ]
                },
                2: {
                    'list': [
                             {
                                 'f': 'sixth',
                                 'price': 6
                             },
                             {
                                 'e': 'fifth',
                                 'price': 5
                             },
                             {
                                 'd': 'fourth',
                                 'price': 4
                             }                             
                    ]
                }
            }
        }
    }
}

I need to order each list by price, ascending. The result should be:

d = {
    'futures': {
        'test': {
            'nested': {
                1: {
                    'list': [
                             {
                                 'a': 'first',
                                 'price': 1
                             },
                             {
                                 'b': 'second',
                                 'price': 2
                             },
                             {
                                 'c': 'third',
                                 'price': 3
                             },                            
                                                         
                    ]
                },
                2: {
                    'list': [
                             {
                                 'd': 'fourth',
                                 'price': 4
                             },
                             {
                                 'e': 'fifth',
                                 'price': 5
                             },
                             {
                                 'f': 'sixth',
                                 'price': 6
                             }                             
                                                         
                    ]
                }
            }
        }
    }
}

None of the questions I’ve found fits my needs because of this particular structure.

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

Is there a way to order it without having to access each previous keys? Because on my project I have cases with more nested keys before the list, so I need a dynamic solution for sorting it.
I mean, I don’t know the exactly path to the list, only the list key.

>Solution :

Make a function to recursively traverse your dict looking for lists, and sort each one based on your criteria:

def find_and_sort_lists(d):

    for value in d.values():

        if isinstance(value, list):
            value.sort(key = lambda nested_d: nested_d['price'])

        if isinstance(value, dict):
            find_and_sort_lists(value)

If it’s a requirement to sort only lists whose key is actually 'list', you can use the following:

def find_and_sort_lists(d):

    for key, value in d.items():

        if key == 'list' and isinstance(value, list):
            value.sort(key = lambda nested_d: nested_d['price'])

        if isinstance(value, dict):
            find_and_sort_lists(value)
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