0

I was trying to sort a list whose element is dictionary. I want to sort my list by the key Route.

Here I did:

MenuList = [
    {
        'Title': [
            {'Description': 'Form Customer Center'},
            {'Route': 'LoanCentre'},
            { 'Menu': [
                {'Parent': 'Customer'},
                {'Order': 4}
            ]}
        ]
    },
    {
        'Title': [
            {'Description': 'Customer'},
            {'Route': 'CustomerCenter'},
            { 'Menu': [
                {'Parent': 'Customer'},
                {'Order': 3}
            ]}
        ]
    }
]
    
print (MenuList.sort())
TypeError: '<' not supported between instances of 'dict' and 'dict'>

Any thought to sort this kind of list please?

I just want to get result of this:

MenuList = [ 
    {
        'Title': [
            {'Description': 'Customer'},
            {'Route': 'CustomerCenter'},
            { 'Menu': [
                {'Parent': 'Customer'},
                {'Order': 3}
            ]}
        ]
    },
    { 
        'Title': [
            {'Description': 'Form Customer Center'},
            {'Route': 'LoanCentre'},
            { 'Menu': [
                {'Parent': 'Customer'},
                {'Order': 4}
            ]}
        ]
    }
]
flaxel
  • 4,173
  • 4
  • 17
  • 30
Houy Narun
  • 1,557
  • 5
  • 37
  • 86
  • Are you aware that the `sort` method has a `key` parameter? Did you research how to use it? – mkrieger1 Dec 16 '20 at 09:52
  • 4
    Does this answer your question? [How do I sort a list of dictionaries by a value of the dictionary?](https://stackoverflow.com/questions/72899/how-do-i-sort-a-list-of-dictionaries-by-a-value-of-the-dictionary) – skuzzy Dec 16 '20 at 09:53
  • Are you sure you don't want to use structures like a DataFrame for your operations? – Claire Dec 16 '20 at 09:53
  • @Tomerikoo, this dictionary was from yaml file conversion using yaml.load() function. https://pypi.org/project/PyYAML/ I am working on that raw dictionary. Thanks – Houy Narun Dec 16 '20 at 10:01

3 Answers3

1

Using the key parameter of list.sort()

MenuList.sort(key=lambda v: v['Title'][1]['Route'])
print(MenuList)
Keldan Chapman
  • 665
  • 5
  • 21
  • This will *probably* work, but it might be worth generalising so that it finds the "route" dictionary rather than relying on it being the 2nd element in the list. – Simon Brahan Dec 16 '20 at 09:58
  • 1
    Yes, even better would be to replace the list of single-key dicts with one dict. Then it would be simply `v['Title']['Route']`. But the question was how to sort by a value, not how to structure data or find elements. – Keldan Chapman Dec 16 '20 at 10:01
1

This should work:

MenuList = [ { 'Title': [ {'Description': 'Form Customer Center'},
                         {'Route': 'LoanCentre'},
           
                         { 'Menu': [ {'Parent': 'Customer'},
                                     {'Order': 4}]}
                        ]
              },

              { 'Title': [ {'Description': 'Customer'},
                           {'Route': 'CustomerCenter'},
                           { 'Menu': [ {'Parent': 'Customer'},
                                       {'Order': 3}]}
                        ]
              }
            ]

MenuList.sort(key=lambda x: x["Title"][2]["Menu"][1]["Order"], reverse=False)
print(MenuList)

Thank you.

0

Try the below

 data = [{'Title': [{'Description': 'Form Customer Center'},
                   {'Route': 'LoanCentre'},
                   {'Menu': [{'Parent': 'Customer'},
                             {'Order': 4}]}
                   ]
         },
        {'Title': [{'Description': 'Customer'},
                   {'Route': 'CustomerCenter'},
                   {'Menu': [{'Parent': 'Customer'},
                             {'Order': 3}]}
                   ]
         }
        ]
data = sorted(data, key=lambda x: x['Title'][1]['Route'])
print(data)

output

   [{'Title': [{'Description': 'Customer'}, {'Route': 'CustomerCenter'}, {'Menu': [{'Parent': 'Customer'}, {'Order': 3}]}]}, {'Title': [{'Description': 'Form Customer Center'}, {'Route': 'LoanCentre'}, {'Menu': [{'Parent': 'Customer'}, {'Order': 4}]}]}]
balderman
  • 22,927
  • 7
  • 34
  • 52