Could someone pls explain? I dont get it. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Could someone pls explain? I dont get it.

words=["I", "love", "SoloLearn"] less =[i for i in words if "o" in i] print(len(min(less, key = lambda x: - len (x)))) Output : 9

8th Apr 2020, 8:02 AM
Stefan Reimer
Stefan Reimer - avatar
6 Answers
+ 3
your key is lambda x: - len(x) it will take the opposite of the length, so: len('love') =4 and len('SoloLearn') =9 but - 9<-4 so the min will be 'SoloLearn' with this key.
8th Apr 2020, 8:23 AM
John Robotane
John Robotane - avatar
+ 2
no, because without a key, python just compare the elements of the list. but strings comparison is an alphabetical comparison. and it looks like capital letters come before lowercase letters so: 'SoloLearn'<'love' but 'Love'<'SoloLearn'
8th Apr 2020, 9:10 AM
John Robotane
John Robotane - avatar
+ 2
Stefan Reimer Run this https://code.sololearn.com/c7S4Mxsbwa9l/?ref=app The letters WE use each have a number assigned to them which the PROGRAMS use. 'S' has a smaller number than 'l', so min(less) returns Sololearn because of the capital 'S', against 'love' which starts with 'l'. Hope this helps
8th Apr 2020, 9:10 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
8th Apr 2020, 8:21 AM
Rik Wittkopp
Rik Wittkopp - avatar
0
Is "sololearn" the output from (min(less)) cause "Love" has a "v"in it?
8th Apr 2020, 8:59 AM
Stefan Reimer
Stefan Reimer - avatar
0
Thx guys that helped alot. I finally got it! Thumbs up!
8th Apr 2020, 12:46 PM
Stefan Reimer
Stefan Reimer - avatar