3

I'd like to compare two lists. I'd like to find elements in the first list that don't have a corresponding entry in the second list (order doesn't matter):

a = ['hi', 'hi', 'bye', 'hi']
b = ['hi', 'hi', 'bye']

So I would like the output to be

c = ['hi']  

since the first list has an extra 'hi' in it that doesn't appear in the second list.

If I do one of the usual techniques, I can use a list comprehension:

[x for x in a if x not in b]

which gives me [], which is not what I want.

Things I've tried involve using the set operator, which have the same outcome, since that operation reduces the members of the list to uniqueness.

This seems like a simple operation. Do I need to enumerate each element in the lists first, and create tuples to compare? Do I need to put them into a Counter dict? All this sounds a little bit like overkill when I just want to a simple comparison of the elements in a list!

Community
  • 1
  • 1
Monica Heddneck
  • 2,973
  • 10
  • 55
  • 89
  • I think it's better if you add more test cases other than the one you put, for example, what's your expected output if `a=[1,2,2,2] and b=[1,2,3,4` ?...will `c=[2,2]` ? – Iron Fist Mar 23 '16 at 18:54
  • 1
    I'd go through the 2nd list in a loop, find that element in the first list and then `pop` it. – roadrunner66 Mar 23 '16 at 18:55
  • I think it's actually the first list that needs to be looped through. See my answer below. – Siwel Mar 23 '16 at 19:27

2 Answers2

7

Counter objects support multi-set operations:

>>> from collections import Counter
>>> a = ['hi', 'hi', 'bye', 'hi']
>>> b = ['hi', 'hi', 'bye']
>>> Counter(a) - Counter(b)
Counter({'hi': 1})

Rebuilding a list from the Counter:

>>> list(counter.elements())
['hi']
wim
  • 338,267
  • 99
  • 616
  • 750
0

You can do it simply without requiring any imports, with a while loop, checking each item:

a = ['hi', 'hi', 'bye', 'hi']
b = ['hi', 'hi', 'bye']
c = []
while a:
    # Get first item (and remove).
    item = a.pop(0)
    if item in b:
        b.remove(item)
    else:
        c.append(item)

print c
Siwel
  • 705
  • 10
  • 25