[Tutor] list comprehensions

Tim Wilson wilson@visi.com
Tue, 20 Aug 2002 22:29:52 -0500


Hi everyone,

I'm wondering if it's possible to implement the following function using
list comprehensions. It's not really a speed issue because it runs in a
split second using the built-in dictionary on my Linux box.

-Tim

def filterLongest(words):
    """
    Filter a list of words for all words that have a length equal to the
    longest word in the list.
    
    Argument:
    words -- List of words to filter
    
    """
    l = []
    maxLength = 0
    for word in words:
        wordLength = len(word)
        if wordLength == maxLength:
            l.append(word)
        elif wordLength > maxLength:
            l = [word]
            maxLength = wordLength
    return l

-- 
Tim Wilson      |   Visit Sibley online:   | Check out:
Henry Sibley HS |  http://www.isd197.org   | http://www.zope.com
W. St. Paul, MN |                          | http://slashdot.org
wilson@visi.com |  <dtml-var pithy_quote>  | http://linux.com