Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i print the longest and shortest words out of a given set of words?

Tags:

python

list

What I want is to write a code to detect the longest and shortest words from those given in a list. I tried to write a code to find the longest word using the number of characters which failed really bad...I'm wondering if there is a simple function or line that I'm missing.

mywords=['how','no','because','sheep','mahogany']

n = len(mywords)

a=0
while a < n:

print(( len( list(mywords[a]))))
a += 1

if a > n:
    break

though i do get a print of the number of characters of each word i cant figure out how to proceed.

PS : id like to know how to create a list from the values obtained as the result from the above function

like image 237
iNuwanS Avatar asked Mar 10 '14 17:03

iNuwanS


1 Answers

You can use max, and min.

>>> max(mywords, key=len)
'mahogany'
>>> min(mywords, key=len)
'no'
like image 149
Jayanth Koushik Avatar answered Oct 26 '22 05:10

Jayanth Koushik