According the original Python documentation:
Both list.sort() and sorted() have a key parameter to specify a
function to be called on each list element prior to making
comparisons.
Note that you can use both list.sort()
or sorted()
:
Python lists have a built-in list.sort() method that modifies the list
in-place. There is also a sorted() built-in function that builds a new
sorted list from an iterable.
You can find some useful examples here.
# take second element for sort
def takeSecond(elem):
return elem[1]
# your list
Nlist = [['naveen',31.5],['murali',44.1],['sai',23.1],['satya',23.1]]
# sort list with key
Nlist.sort(key=takeSecond)
# print list
print('Sorted list:', Nlist)
Out:
Sorted list: [['sai', 23.1], ['satya', 23.1], ['naveen', 31.5], ['murali', 44.1]]