What does "key=len" default variable do in this line of code ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What does "key=len" default variable do in this line of code ?

words =[ "hi","solo"] more = [x*len(x) for x in words] print(len(max(more,key =len))) '''without key=len ooutput is same then why to use this default variable'''

3rd Sep 2018, 8:15 AM
Meet Rajpopat
Meet Rajpopat - avatar
2 Answers
+ 4
because 'solo' > 'hi' returns True. You will probably understand the difference if you compare 'aaaa' and 'bb'
3rd Sep 2018, 10:03 AM
Mert Yazıcı
Mert Yazıcı - avatar
+ 3
you can use the max method according to your need.max method use for find the maximum value in your list,set etc.but remeber that maximum value which you are finding can be compare two integers it can be the length of two strings which you are comparing etc.here max use for compare the length of two strings and key is telling to the max function that you need to compare the lengths of strings which are present in the list.
3rd Sep 2018, 4:55 PM
Maninder $ingh
Maninder $ingh - avatar