title: Fastest way to re-read a file in Python?
body: <p>I've got a file which has a list of names and their position(start - end).</p>
  
  <p>My script iterates over that file and per name it reads another file with info to check if that line is between those positions and then calculates something out of that.</p>
  
  <p>At the moment it reads the whole second file(60MB) line by line checking if it's between the start / end. For every name in the first list(approx 5000). What's the fastest way to collect the data that's between those parameters instead of rereading the whole file 5000 times?</p>
  
  <p>Sample code of the second loop:</p>
  
  <pre><code>for line in file:
      if int(line.split()[2]) &gt;= start and int(line.split()[2]) &lt;= end:
          Dosomethingwithline():
  </code></pre>
  
  <p>EDIT: Loading the file in a list above the first loop and iterating over that improved the speed.</p>
  
  <pre><code>with open("filename.txt", 'r') as f:
      file2 = f.readlines()
  for line in file:
      [...]
      for line2 in file2:
         [...]
  </code></pre>