find the "overlap" between 2 python lists
<p>Given 2 lists:</p>

<pre><code>a = [3,4,5,5,5,6]
b = [1,3,4,4,5,5,6,7]
</code></pre>

<p>I want to find the "overlap":</p>

<pre><code>c = [3,4,5,5,6]
</code></pre>

<p>I'd also like it if i could extract the "remainder" the part of a and b that's not in c.</p>

<pre><code>a_remainder = [5,]
b_remainder = [1,4,7,]
</code></pre>

<p>Note:
a has three 5's in it and b has two.
b has two 4's in it and a has one.</p>

<p>The resultant list c should have two 5's (limited by list b) and one 4 (limited by list a).</p>

<p>This gives me what i want, but I can't help but think there's a much better way.</p>

<pre><code>import copy

a = [3,4,5,5,5,6]
b = [1,3,4,4,5,5,6,7]

c = []
for elem in copy.deepcopy(a):
    if elem in b:
        a.pop(a.index(elem))
        c.append(b.pop(b.index(elem)))

# now a and b both contain the "remainders" and c contains the "overlap"
</code></pre>

<p>On another note, what is a more accurate name for what I'm asking for than "overlap" and "remainder"?</p>

<p>edit: In the original post, I  had mistakenly typed that the desired result was a_remainder == [4,] -- Andrew Jaffe correctly pointed out that a_remainder should be [5,].</p>
