title: Efficiently extract lines containing a string in Python
body: <p>Sometimes I need to get only lines containing a certain string from a text file (e.g., while parsing a logfile). I usually do it this way:</p>
  
  <pre class="lang-python prettyprint-override"><code>with open(TEXTFILENAME,'r') as f:
    contents = f.readlines()
  
  targets = [s for s in contents if FINDSTRING in s]
  </code></pre>
  
  <p>However, I saw there's a possible two-liner:</p>
  
  <pre><code>with open(TEXTFILENAME,'r') as f:
    targets = [s for s in f.readlines() if FINDSTRING in s]
  </code></pre>
  
  <p>I wonder if the second method is more efficient, whether the <code>readlines()</code> function in this case act as an iterator of sorts.</p>