17

I would like to return values from both lists that not in the other one:

bar = [ 1,2,3,4,5 ]
foo = [ 1,2,3,6 ]

returnNotMatches( a,b )

would return

[[ 4,5 ],[ 6 ]]
3
  • 4
    Great. What have you tried so far
    – idjaw
    Mar 1, 2016 at 1:31
  • What should the result for bar = [1,2,3,4,5,6]; foo = [1,2,3,5,6] be?
    – mgilson
    Mar 1, 2016 at 1:34
  • 2
    Are you tied to using lists? Would a set be possible? Mar 1, 2016 at 1:34

5 Answers 5

54

One of the simplest and quickest is:

new_list = list(set(list1).difference(list2))

BONUS! If you want an intersection (return the matches):

new_list = list(set(list1).intersection(list2))
3
  • 7
    hey @user3885769 I believe your two lines of code are identical
    – BeeGee
    Sep 11, 2019 at 15:11
  • 2
    This is significantly faster than iterating through both lists as per the chosen answer. Comparing two lists with ~200,000 uuids went from 5+ mins to subsecond for me.
    – minus34
    May 18, 2020 at 6:53
  • 1
    If you use list() constructor, you don't need to use [].
    – hui chen
    Jan 13, 2021 at 14:24
33

Just use a list comprehension:

def returnNotMatches(a, b):
    return [[x for x in a if x not in b], [x for x in b if x not in a]]
5

This should do

def returnNotMatches(a, b):
    a = set(a)
    b = set(b)
    return [list(b - a), list(a - b)]

And if you don't care that the result should be a list you could just skip the final casting.

6
  • This will discard duplicates in a or b that are not matched by the other. Mar 1, 2016 at 1:36
  • It will, but comparing matches with duplicates makes little sense.
    – Łukasz
    Mar 1, 2016 at 1:36
  • Unless it has sense... I don't think you can assert that it doesn't have a purpose without knowing the application. Mar 1, 2016 at 1:37
  • 1
    "I would like to return values from both lists that not in the other one" clearly means to treat the list as a set.
    – Łukasz
    Mar 1, 2016 at 1:40
  • With the literal wording, [4, 5, 6] would be the desired output. I added the comment to the question about sets because it might be that they askee should be using a set as their data structure to begin with. Mar 1, 2016 at 1:42
1

I might rely on the stdlib here...

from itertools import tee, izip
def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = tee(iterable)
    next(b, None)
    return izip(a, b)

import difflib

def returnNotMatches(a, b):
    blocks = difflib.SequenceMatcher(a=a, b=b).get_matching_blocks()
    differences = []
    for b1, b2 in pairwise(blocks):
        d1 = a[b1.a + b1.size: b2.a]
        d2 = b[b1.b + b1.size: b2.b]
        differences.append((d1, d2))
    return differences

print returnNotMatches([ 1,2,3,4,5 ], [ 1,2,3,6 ])

which prints: [([4, 5], [6])]

This compares the sequences as streams and finds the differences in the streams. It takes order into account, etc. If order and duplicates don't matter, then sets are by far the way to go (so long as the elements can be hashed).

9
  • o.o While difflib is a cool library, is this a bot post? at first glance this code looked real, and pairwise is an interesting function, but it isn't used, differences isn't used, and the code throws a ValueError... Mar 1, 2016 at 1:46
  • @JaredGoguen -- I disagree... Finding the matching blocks of in the sequence when the sequences are different lengths and the length of the different blocks of unmatching values are different is quite non-trivial (especially considering that the answer isn't even necessarily unique). Hence the existence of difflib :-)
    – mgilson
    Mar 1, 2016 at 1:50
  • @JaredGoguen --Thanks, obviously hacking code together and not keeping things updated...
    – mgilson
    Mar 1, 2016 at 1:51
  • NameError: name 'size' is not defined still Mar 1, 2016 at 1:51
  • I'm personally curious about that code (which is why the successive comments), not trying to nitpick. I think you might be answering the wrong question though, the answer by zondo is Pythonic and correct (albeit, possibly inefficient, but readability should not often be sacrificed for efficiency in Python). Mar 1, 2016 at 1:54
0

You could use a list comprehension and zip

''.join([i[0] for i in zip(a, a.lower()) if i[0] == i[1]])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.