How to select words of equal max length from a text document
<p>I am trying to write a program to read a text document and output the longest word in the document. If there are multiple longest words (i.e., all of equal length) then I need to output them all in the same order in which they occur. For example, if the longest words were dog and cat your code should produce:</p>

<p>dog cat</p>

<p>I am having trouble finding out how to select numerous words of equal max length and print them. This is as far as I've gotten, I am just struggling to think of how to select all words with equal max length:</p>

<p>open the file for reading</p>

<pre><code>fh = open('poem.txt', 'r')

longestlist = []  
longestword = ''  

for line in fh:
    words = (line.strip().split(' '))  
    for word in words:  
        word = ''.join(c for c in word if c.isalpha())  
        if len(word) &gt; (longestword):  
            longest.append(word)

for i in longestlist:  
    print i  
</code></pre>

<p>What you need to do is to keep a list of all the longest words you've seen so far and keep the longest length. So for example, if the longest word so far has the length 5, you will have a list of all words with 5 characters in it. As soon as you see a word with 6 or more characters, you will clear that list and only put that one word in it and also update the longest length. If you visited words with same length as the longest you should add them to the list.</p>

<p>P.S. I did not put the code so you can do it yourself.</p>

python string maxlength