Given a list of names, how to sort it by lambda function in python? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Given a list of names, how to sort it by lambda function in python?

29th Oct 2017, 5:24 AM
Ajeya Bhat
Ajeya Bhat - avatar
3 Answers
+ 5
You should specify in what coding language you want to get an answer ^^ Lambda function are annonym (unnamed) functions, and anything you can do with a lambda one, you can do it with named ones... In Python, named (classic) function declaration look as: def dosmething(arg): return "I will return the argument: "+str(arg)+" enclosed in a new string" ... wich can also be write: gratter = lambda arg: "I will return the argument: "+str(arg)+" enclosed in a new string" And you can sort list with the sorted() built-in function or the sort() list method which both accept a callback function to handle the type of sort wanted as argument (in Python this is an named 'key' argument, but you also could use the 'reverse' named argument, even you can reverse a list simply through slicing notation: myList = myList[::-1] -- in Javascript, the sort() method of Array get a first argument expecting a callback function handling two items and returning negative, null or positive value to order themselves: this function is called as many times necessary to order the array)
29th Oct 2017, 6:11 AM
visph
visph - avatar
+ 5
names = ["Rishikesh", "aman", "Ajay",u "Hemkesh", "sandeep", "Darshan", "Virendra", "Shwetabh"] names3 = sorted(names, key=lambda name:name.lower()) names3 ['Ajay', 'aman', 'Darshan', 'Hemkesh', 'Rishikesh', 'sandeep', 'Shwetabh', 'Virendra']
29th Oct 2017, 8:54 AM
Oma Falk
Oma Falk - avatar
+ 3
You can do like this, I found these when I was searching lambda if statements https://code.sololearn.com/cQPE7T9JeG3t/?ref=app
29th Oct 2017, 7:02 AM
Ferhat Sevim
Ferhat Sevim - avatar