I have two lists in Python, like these:
temp1 = ['One', 'Two', 'Three', 'Four']
temp2 = ['One', 'Two']
I need to create a third list with items from the first list which aren't present in the second one. From the example I have to get:
temp3 = ['Three', 'Four']
Are there any fast ways without cycles and checking?
this could be even faster than Mark's list comprehension:
In case you want the difference recursively, I have written a package for python: https://github.com/seperman/deepdiff
Installation
Install from PyPi:
Example usage
Importing
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:
List difference ignoring order or duplicates: (with the same dictionaries as above)
List that contains dictionary:
Sets:
Named Tuples:
Custom objects:
Object attribute added:
If you are really looking into performance, then use numpy!
Here is the full notebook as a gist on github with comparison between list, numpy, and pandas.
https://gist.github.com/denfromufa/2821ff59b02e9482be15d27f2bbd4451
You could use a naive method if the elements of the difflist are sorted and sets.
or with native set methods:
Naive solution: 0.0787101593292
Native set solution: 0.998837615564
This can be solved with one line. The question is given two lists (temp1 and temp2) return their difference in a third list (temp3).