Reputation:
I have a list called myList. What i am trying to achieve is that, i want to sort the list of list based on the value of age key. I know it can be done with sorted function, but i do not know how to write function for the key. any one can suggest me idea to solve my problem.
myList = [ {'john':{'age':30 ,'salary':600000}}, {'mullar':{'age':25 ,'salary':250000}},
{'todd':{'age':40 ,'salary':300000}},{'rolex':{'age':20 ,'salary':450000}},
{'ron':{'age':20 ,'salary':500000}},{'gilex':{'age':30 ,'salary':450000}},
{'larrat':{'age':41 ,'salary':350000}},{'fyoid':{'age':24 ,'salary':400000}},
{'devon':{'age':33 ,'salary':600000}},{'dron':{'age':20 ,'salary':200000}}
]
Upvotes: 0
Views: 153
Reputation: 152637
The problem is that your list contains a dictionary containing a dictionary. You need to somehow get the inner dictionary. This is generally solved by getting the next
item: next(iter(subdict.values()))
. Getting the 'age'
value thereafter isn't complicated, just index with 'age'
:
>>> sorted(myList, key=lambda x: next(iter(x.values()))['age'])
[{'rolex': {'age': 20, 'salary': 450000}},
{'ron': {'age': 20, 'salary': 500000}},
{'dron': {'age': 20, 'salary': 200000}},
{'fyoid': {'age': 24, 'salary': 400000}},
{'mullar': {'age': 25, 'salary': 250000}},
{'john': {'age': 30, 'salary': 600000}},
{'gilex': {'age': 30, 'salary': 450000}},
{'devon': {'age': 33, 'salary': 600000}},
{'todd': {'age': 40, 'salary': 300000}},
{'larrat': {'age': 41, 'salary': 350000}}]
Instead of a lambda
you could also def
ine a function:
def age(somedict):
inner_dict, = somedict.values() # or inner_dict = next(iter(somedict.values()))
return inner_dict['age']
Works as well:
>>> sorted(myList, key=age)
[... same as above ...]
However I personally would flatten the dictionaries first (either as single dictionaries or collections.namedtuple
s or if you have access to pandas
then as DataFrames
):
myList2 = [{'name': key, 'age': value['age'], 'salary': value['salary']}
for dct in myList
for key, value in dct.items()]
print(myList2)
#[{'age': 30, 'name': 'john', 'salary': 600000},
# {'age': 25, 'name': 'mullar', 'salary': 250000},
# {'age': 40, 'name': 'todd', 'salary': 300000},
# {'age': 20, 'name': 'rolex', 'salary': 450000},
# {'age': 20, 'name': 'ron', 'salary': 500000},
# {'age': 30, 'name': 'gilex', 'salary': 450000},
# {'age': 41, 'name': 'larrat', 'salary': 350000},
# {'age': 24, 'name': 'fyoid', 'salary': 400000},
# {'age': 33, 'name': 'devon', 'salary': 600000},
# {'age': 20, 'name': 'dron', 'salary': 200000}]
Which simplifies the key
-function:
sorted(myList2, key=lambda x: x['age']) # or operator.itemgetter('age')
[{'age': 20, 'name': 'rolex', 'salary': 450000},
{'age': 20, 'name': 'ron', 'salary': 500000},
{'age': 20, 'name': 'dron', 'salary': 200000},
{'age': 24, 'name': 'fyoid', 'salary': 400000},
{'age': 25, 'name': 'mullar', 'salary': 250000},
{'age': 30, 'name': 'john', 'salary': 600000},
{'age': 30, 'name': 'gilex', 'salary': 450000},
{'age': 33, 'name': 'devon', 'salary': 600000},
{'age': 40, 'name': 'todd', 'salary': 300000},
{'age': 41, 'name': 'larrat', 'salary': 350000}]
Even easier with DataFrame
s:
>>> import pandas as pd
>>> df = pd.DataFrame(myList2)
>>> df.sort_values('age')
age name salary
3 20 rolex 450000
4 20 ron 500000
9 20 dron 200000
7 24 fyoid 400000
1 25 mullar 250000
0 30 john 600000
5 30 gilex 450000
8 33 devon 600000
2 40 todd 300000
6 41 larrat 350000
Upvotes: 1
Reputation: 2047
def getAge(d):
salary = list(d.values())[0]['age']
return salary
#d.values() => dict_values([{'age': 30, 'salary': 600000}])
#list(d.values()) => [{'age': 30, 'salary': 600000}]
#list(d.values())[0] => {'age': 30, 'salary': 600000}
##list(d.values())[0]['age'] => 30
print(sorted(myList,key=getAge))
RESULT
[{'rolex': {'age': 20, 'salary': 450000}}, {'ron': {'age': 20, 'salary': 500000}}, {'dron': {'age': 20, 'salary': 200000}}, {'fyoid': {'age': 24, 'salary': 400000}}, {'mullar': {'age': 25, 'salary': 250000}}, {'john': {'age': 30, 'salary': 600000}}, {'gilex': {'age': 30, 'salary': 450000}}, {'devon': {'age': 33, 'salary': 600000}}, {'todd': {'age': 40, 'salary': 300000}}, {'larrat': {'age': 41, 'salary': 350000}}]
Upvotes: 1