Sort nested list of dict by it's dict value

Viewed 74

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.

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.

1 Answers

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)
Related