In Python, what is the best way to compute the difference between two lists?
example
A = [1,2,3,4]
B = [2,5]
A - B = [1,3,4]
B - A = [5]
In Python, what is the best way to compute the difference between two lists?
example
A = [1,2,3,4]
B = [2,5]
A - B = [1,3,4]
B - A = [5]
You would want to use a
set
instead of alist
.Use
set
if you don't care about items order or repetition. Use list comprehensions if you do:The above examples trivialized the problem of calculating differences. Assuming sorting or de-duplication definitely make it easier to compute the difference, but if your comparison cannot afford those assumptions then you'll need a non-trivial implementation of a diff algorithm. See difflib in the python standard library.
A - B = [[1, 3, 4]]
If the order does not matter, you can simply calculate the set difference:
When having a look at TimeComplexity of In-operator, in worst case it works with O(n). Even for Sets.
So when comparing two arrays we'll have a TimeComplexity of O(n) in best case and O(n^2) in worst case.
An alternative (but unfortunately more complex) solution, which works with O(n) in best and worst case is this one:
e.g.
In case you want the difference recursively going deep into items of your list, I have written a package for python: https://github.com/erasmose/deepdiff
Installation
Install from PyPi:
If you are Python3 you need to also install:
Example usage
Same object returns empty
Type of an item has changed
Value of an item has changed
Item added and/or removed
String difference
String difference 2
Type change
List difference
List difference 2: Note that it DOES NOT take order into account
List that contains dictionary: