title: Faster or more memory-efficient solution in Python for this Codejam problem
body: <p>I tried my hand at this <a href="http://code.google.com/codejam/contest/dashboard?c=438101#s=p0&amp;a=0" rel="nofollow noreferrer">Google Codejam Africa</a> problem (the contest is already finished, I just did it to improve my programming skills).</p>
  <blockquote>
  <p><strong>The Problem:</strong></p>
  <p>You are hosting a party with G guests
  and notice that there is an odd number
  of guests! When planning the party you
  deliberately invited only couples and
  gave each couple a unique number C on
  their invitation. You would like to
  single out whoever came alone by
  asking all of the guests for their
  invitation numbers.</p>
  <p><strong>The Input:</strong></p>
  <p>The first line of input gives the number of cases, N.
  N test cases follow. For each test case there will be:</p>
  <ul>
  <li>One line containing the value G the
  number of guests.</li>
  <li>One line containing
  a space-separated list of G integers.
  Each integer C indicates the
  invitation code of a guest. Output</li>
  </ul>
  <p>For each test case, output one line
  containing &quot;Case #x: &quot; followed by the
  number C of the guest who is alone.</p>
  <p><strong>The Limits:</strong></p>
  <ul>
  <li>1 ≤ N ≤ 50</li>
  <li>0 &lt; C ≤ 2147483647</li>
  </ul>
  <p><strong>Small dataset</strong></p>
  <p>3 ≤ G &lt; 100</p>
  <p><strong>Large dataset</strong></p>
  <p>3 ≤ G &lt; 1000</p>
  </blockquote>
  <p><strong>Sample Input:</strong></p>
  <pre><code>3
  3
  1 2147483647 2147483647
  5
  3 4 7 4 3
  5
  2 10 2 10 5
  </code></pre>
  <p><strong>Sample Output:</strong></p>
  <pre><code>Case #1: 1
  Case #2: 7
  Case #3: 5
  </code></pre>
  <p>This is the solution that I came up with:</p>
  <pre><code>with open('A-large-practice.in') as f:
      lines = f.readlines()
  
  with open('A-large-practice.out', 'w') as output:
      N = int(lines[0])
      for testcase, i in enumerate(range(1,2*N,2)):
          G = int(lines[i])
          for guest in range(G):
              codes = map(int, lines[i+1].split(' '))
              alone = (c for c in codes if codes.count(c)==1)
          output.write(&quot;Case #%d: %d\n&quot; % (testcase+1, alone.next()))
  </code></pre>
  <p>It runs in 12 seconds on my machine with the <a href="http://code.google.com/codejam/contest/dashboard?c=438101#s=p0&amp;a=0" rel="nofollow noreferrer">large input</a>.</p>
  <p>Now, my question is, can this solution be improved in Python to run in a shorter time or use less memory? The <a href="http://code.google.com/codejam/contest/dashboard?c=438101#s=a&amp;a=0" rel="nofollow noreferrer">analysis</a> of the problem gives some pointers on how to do this in Java and C++ but I can't translate those solutions back to Python.</p>
  <p><strong>Edit:</strong></p>
  <p>Incorporating the tips from Alex Martelli and IVlad below I now have this solution which runs in 0.079s:</p>
  <pre><code>with open('A-large-practice.in') as f:
      lines = f.readlines()
  
  with open('A-large-practice.out', 'w') as output:
      N = int(lines[0])
      for testcase, i in enumerate(range(1,2*N,2)):
          codes = map(int, lines[i+1].split(' '))
          alone = 0
          for c in codes: alone ^= c
          output.write(&quot;Case #%d: %d&quot; % (testcase+1, alone))
  </code></pre>