Python - printing multiple shortest and longest words from a list

后端 未结 6 1374
旧时难觅i
旧时难觅i 2021-01-14 05:25


I need to go through a list and print the longest words in it. I can do this for just one word, but can\'t figure out how to print more than one, if there are two words

6条回答
  •  一整个雨季
    2021-01-14 05:46

    Can always use a lambda function as well

    longest = len(sorted(list, key=len)[-1])
    filter(lambda item: len(item) == longest, list)
    

    will yield the longest words

    shortest = len(sorted(list, key=len)[0])
    filter(lambda item: len(item) == shortest, list)
    

    Will yield the shortest words

提交回复
热议问题