Let's say I have two lists, l1
and l2
. I want to perform l1 - l2
, which returns all elements of l1
not in l2
.
I can think of a naive loop approach to doing this, but that is going to be really inefficient. What is a pythonic and efficient way of doing this?
As an example, if I have l1 = [1,2,6,8] and l2 = [2,3,5,8]
, l1 - l2
should return [1,6]
Alternate Solution :
As an alternative, you may also use
filter
with the lambda expression to get the desired result. For example:Performance Comparison
Here I am comparing the performance of all the answers mentioned here. As expected, Arkku's
set
based operation is fastest.Arkku's Set Difference - First (0.124 usec per loop)
Daniel Pryden's List Comprehension with
set
lookup - Second (0.302 usec per loop)Donut's List Comprehension on plain list - Third (0.552 usec per loop)
Moinuddin Quadri's using
filter
- Fourth (0.972 usec per loop)Akshay Hazari's using combination of
reduce
+filter
- Fifth (3.97 usec per loop)PS:
set
do not maintain the order and remove the duplicate elements from the list. Hence, do not use set difference if you need any of these.Python has a language feature called List Comprehensions that is perfectly suited to making this sort of thing extremely easy. The following statement does exactly what you want and stores the result in
l3
:l3
will contain[1, 6]
.Hope this helps!
One way is to use sets:
Expanding on Donut's answer and the other answers here, you can get even better results by using a generator comprehension instead of a list comprehension, and by using a
set
data structure (since thein
operator is O(n) on a list but O(1) on a set).So here's a function that would work for you:
The result will be an iterable that will lazily fetch the filtered list. If you need a real list object (e.g. if you need to do a
len()
on the result), then you can easily build a list like so:Use the Python set type. That would be the most Pythonic. :)
Also, since it's native, it should be the most optimized method too.
See:
http://docs.python.org/library/stdtypes.html#set
http://docs.python.org/library/sets.htm (for older python)