Sort by list in list in python? -


this question has answer here:

i have list of type :

l = [{"id":"21", "region" :['2', '6', '4']}, {"id":"12", "region" :['1', '3', '8']}] 

i want sort list on "region" field, , @ 2nd index. :

l = [{"id":"21", "region" :['2', **'6'**, '4']}, {"id":"12", "region" :['1', **'3'**, '8']}] 

how do ? aware of itemgetter. couldn't also.

you can use list.sort() key argument, passing lambda expression key argument -

in [45]: l = [{"id":"21", "region" :['2', '6', '4']}, {"id":"12", "region" :['1', '3', '8']}]  in [46]: l.sort(key=lambda x: x['region'][1])  in [47]: l out[47]: [{'id': '12', 'region': ['1', '3', '8']},  {'id': '21', 'region': ['2', '6', '4']}] 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -