Python :sort List Of Dictionaries Based On Key
I have a list of dictionaries [{'abc':1},{'abcd':2},{'ab':1}] Based on length of key list has to sorted [{'abcd':2},{'abc':1},{'ab':1}]
Solution 1:
If you want to sort by the longest key:
l = [{'abc':1},{'abcd':2},{'ab':1}]
l.sort(key=lambda x: len(next(iter(x))), reverse=True)
print(l)
Post a Comment for "Python :sort List Of Dictionaries Based On Key"