title: Python: More efficient way to get the intersection of two tuples?
body: <p>I'm working on a project at the moment, for which I have to process &gt;1 Billion lines.
  This file consists of 4 tags that contains url's as follows:</p>
  <pre><code>&lt;stackoverflow.com&gt; &lt;stackoverflow.org&gt; &lt;stackoverflow.net&gt; &lt;stackoverflow.biz&gt;
  </code></pre>
  <p>I extract the lines from a txt file, after which I have to get the content of the 1 &amp; 3 tags to feed them into another function</p>
  <pre><code>with open(&quot;file.txt&quot;, &quot;r&quot;) as f:
      for i in f:
          tag1 = i.split(&quot;&lt;&quot;)[:2][1][:-2]
          tag2 = i.split(&quot;&lt;&quot;)[:5][3][:-2]
  </code></pre>
  <p>I feed these extracted tags into a function, I named it tupleFunction, that returns a number of tuples like:</p>
  <p>from tag1:</p>
  <pre><code>('stackoverflow.com/123456','stackoverflow.com',stackoverflow.com') 
  ('stackoverflow.com/1234567','stackoverflow.com',stackoverflow.com') 
  ('stackoverflow.com/12345678','stackoverflow.com',stackoverflow.com') 
  </code></pre>
  <p>from tag2:</p>
  <pre><code>('stackoverflow.com/654321','stackoverflow.com',stackoverflow.com') 
  ('stackoverflow.com/7654321','stackoverflow.com',stackoverflow.com') 
  ('stackoverflow.com/123456','stackoverflow.com',stackoverflow.com')
  </code></pre>
  <p>From the first element of the tuple, I need to get the intersection of both tuples which is in this case:</p>
  <pre><code>('stackoverflow.com/123456')
  </code></pre>
  <p>I reach this result and append it in a text file by running this code:</p>
  <pre><code>a = [f for f, *_ in tuple1]
  b = [f for f, *_ in tuple2]
  c = str(set(a).intersection(b))[2:-2]
  with open(&quot;res.txt&quot;, &quot;a&quot;) as output:
     output.write(c)
  </code></pre>
  <p>The code all together looks like:</p>
  <pre><code>with open(&quot;file.txt&quot;, &quot;r&quot;) as f:
      for i in f:
          tag1 = i.split(&quot;&lt;&quot;)[:2][1][:-2]
          tag2 = i.split(&quot;&lt;&quot;)[:5][3][:-2]
  
          tuple1 = tupleFunction(tag1)
          tuple2 = tupleFunction(tag2)
  
          a = [f for f, *_ in tuple1]
          b = [f for f, *_ in tuple2]
          c = str(set(a).intersection(b))[2:-2]
          with open(&quot;res.txt&quot;, &quot;a&quot;) as output:
               output.write(c)
  </code></pre>
  <p>My question is: Is there a more efficient way to perform the exact process? my current code needs a month to finish :-(</p>
  <p>Help is highly appreciated!</p>