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
You can use max
, and min
.
>>> max(mywords, key=len)
'mahogany'
>>> min(mywords, key=len)
'no'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With