Find difference in list with identical values
<p>I need to be able to find differences in list that may have identical values to one another besides two added elements</p>

<p>example</p>

<pre><code>a = ['cool task', 'b', 'another task', 'j', 'better task', 'y']
b = ['cool task', 'b', 'a task', 'j', 'another task', 'j', 'better task', 'y']
</code></pre>

<p>How my problem is, both <code>'a task'</code> and <code>'another task'</code> both are followed by a <code>'j'</code></p>

<pre><code>[x for x in b if x not in a]
['a task']
</code></pre>

<p>Because both <code>a</code> and <code>b</code> contain <code>'j'</code>, it is removed from the list.</p>

<p>How would I make so that I end up with </p>

<pre><code>['a task', 'j']
</code></pre>

<p>it works as you want:</p>

<pre><code>#!/usr/bin/env python
# -*- coding: utf-8 -*-


def difference(a, b):
    a, b = (lambda x, y: (y, x) if len(set(x)) &gt; len(set(y)) else (x, y)) (a, b)
    a_result = list(a)
    b_result = list(b)

    for z in range(len(a)):
        if a[z] in b:
            a_result.remove(a[z])
            b_result.remove(a[z])

    return a_result, b_result 
    # or
    # return a_result if len(set(a_result)) &gt; len(set(b_result)) else b_result


def main():
    a = ['cool task', 'b', 'another task', 'j', 'better task', 'y']
    b = ['cool task', 'b', 'a task', 'j', 'another task', 'j', 'better     task', 'y']
    print(difference(a, b))


if __name__ == "__main__":
    main()
</code></pre>

python