Python sorting a list of dictionaries by the value of a key -


sorry first post has question. did searching , couldn't find way sort list of dictionaries based on 1 key.

assume have list of dictionaries

[0]{number:123, name:bill, age:32, sex:male} [1]{number:93, name:billy, age:23, sex:male} [2]{number:113, name:billie, age:32, sex:female} [3]{number:8, name:wills, age:3, sex: male} [4]{number:8, name:wills, age:4, sex: male} [5]{number:8, name:wills, age:5, sex: male} [6]{number:8, name:wills, age:6, sex: male} 

i'd sort or iterate on list on number:value , age:value new list this

[0]{number:8, name:wills, age:3, sex: male} [1]{number:8, name:wills, age:4, sex: male} [2]{number:8, name:wills, age:5, sex: male} [3]{number:8, name:wills, age:6, sex: male} [4]{number:93, name:billy, age:23, sex:male} [5]{number:113, name:billie, age:32, sex:female} [6]{number:123, name:bill, age:32, sex:male} 

so far i've been able sort key inside list, sorted dict entry within list, , kept list indexes same. wasn't wanted.

sorted_data_list = sorted(data_list, key = lambda x: x['number'])  

edit: made list contiguous.

change key lambda x: (x['number'], x['age'])

this creates tuple, , tuples sorted first element, second element.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -