Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Python by (45.3k points)

I have the following Python data structures:

data1 = [{'name': u'String 1'}, {'name': u'String 2'}]

data2 = [{'name': u'String 1'}, {'name': u'String 2'}, {'name': u'String 3'}]

I'm looking for the best way to get the delta between the two lists. Is there anything in Python that's as convenient as the JavaScript Underscore.js (_.difference) library?

1 Answer

0 votes
by (16.8k points)

Use itertools.filterfalse:

import itertools

r = list(itertools.filterfalse(lambda x: x in data1, data2))

  + list(itertools.filterfalse(lambda x: x in data2, data1))

assert r == [{'name': 'String 3'}]

Browse Categories

...