[Tutor] Finding the shortest word in a list of words

Emad Nawfal (عماد نوفل) emadnawfal at gmail.com
Tue Jan 20 03:18:45 CET 2009


Hello tutors,
I need to find the shortest / longest word(s) in a sequence of words. I've
done this and it works, but I'm wondering whether this is a good way:
>>> words = "man woman children he".split()
>>> words
['man', 'woman', 'children', 'he']
>>> lens = [len(word) for word in words]
>>> lens
[3, 5, 8, 2]
>>> for word in words:
...     if len(word) == min(lens): print word
...
he
>>>


-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.....محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington

--------------------------------------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090119/6baefed6/attachment-0001.htm>


More information about the Tutor mailing list