 <p>The <code>readlines()</code> method of a file object returns a list of the lines of the file <em>including the newline characters</em>. Your checks compare with strings not including newline characters, so they always fail.</p> <p>One way of getting rid of the newline characters is</p> <pre><code>lines = (line.strip() for line in open("out2.txt")) </code></pre> <p>(Note that you don't need <code>readlines()</code> -- you can directly iterate over the file object itself.)</p>