Hi Alfonso,<br><br>I see you&#39;ve had some responses yet -- I&#39;ve not read them all, and am just posting the following suggestion you might want to look at:<br><br># read lines with &quot;keys&quot; into a list <br>selected_keys=open(&#39;A.txt&#39;, &#39;r&#39;).readlines()<br>
# read all data records into another list<br>records=open(&#39;B.txt&#39;, &#39;r&#39;).readlines()<br><br># Now use a list comprehension to return the required entries, the i+1th entries for all i indexes in the records<br>
# list that corresponds to a key in the keys list:<br>selected_values = [(records[i], records[i+1]) for i, row in enumerate(records) if row in selected_keys]<br><br># The above returns both the key and the value, in a tuple, if you just want the value rows only then the above becomes:<br>
#selected_values = [records[i+1] for i, row in enumerate(records) if row in selected_keys]<br><br># Finally print the result.<br>print selected_values<br><br>You&#39;ll note I read both files into memory, even though you say your files are largish.  I don&#39;t consider 500MB to be very large in this day and age of 4+GB PC&#39;s, which is why I&#39;ve basically ignored the &quot;large&quot; issue.  If this is not true in your case then you&#39;ll have to post back. <br>
<br>Good luck,<br><br>Walter<br>