by
0
5
5,581
0
Top 1% !
Pearl of Wisdom
Specified
Refined
OpenSource
Popularity: 1345th place

Published on:

No tags for this snippet yet.
BrowserChrome
Chrome Version25.0.1364.152
LanguagePython
LicenseMIT_X11
OSWindows
Windows VersionWindowsVista

sort list using python lambda expression

Using lambda function it's easy to sort data in array (or list) based on specific object attribute.
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/901/sort-list-using-python-lambda-expression?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
#!/usr/bin/python import os import sys import string machineListStatus = list() for i in range(1, 10): currMachine = { 'machine' : 'name' + str(i), 'sizePercentage' : i, } machineListStatus.append(currMachine) # Data ordered as: # 1, 2, 3, .. 10 print currMachine['sizePercentage'] machineListStatus = sorted(machineListStatus, key=lambda x: x['sizePercentage'], reverse=True) # Data ordered as: # 10, 9, .., 1 for elem in machineListStatus: print elem['sizePercentage']
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus