Remove all the elements that occur in one list from another

后端 未结 7 683
旧巷少年郎
旧巷少年郎 2020-11-21 23:45

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

7条回答
  •  再見小時候
    2020-11-22 00:22

    As an alternative, you may also use filter with the lambda expression to get the desired result. For example:

    >>> l1 = [1,2,6,8]
    >>> l2 = set([2,3,5,8])
    
    #     v  `filter` returns the a iterator object. Here I'm type-casting 
    #     v  it to `list` in order to display the resultant value
    >>> list(filter(lambda x: x not in l2, l1))
    [1, 6]
    

    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)

      mquadri$ python -m timeit -s "l1 = set([1,2,6,8]); l2 = set([2,3,5,8]);" "l1 - l2"
      10000000 loops, best of 3: 0.124 usec per loop
      
    • Daniel Pryden's List Comprehension with set lookup - Second (0.302 usec per loop)

      mquadri$ python -m timeit -s "l1 = [1,2,6,8]; l2 = set([2,3,5,8]);" "[x for x in l1 if x not in l2]"
      1000000 loops, best of 3: 0.302 usec per loop
      
    • Donut's List Comprehension on plain list - Third (0.552 usec per loop)

      mquadri$ python -m timeit -s "l1 = [1,2,6,8]; l2 = [2,3,5,8];" "[x for x in l1 if x not in l2]"
      1000000 loops, best of 3: 0.552 usec per loop
      
    • Moinuddin Quadri's using filter - Fourth (0.972 usec per loop)

      mquadri$ python -m timeit -s "l1 = [1,2,6,8]; l2 = set([2,3,5,8]);" "filter(lambda x: x not in l2, l1)"
      1000000 loops, best of 3: 0.972 usec per loop
      
    • Akshay Hazari's using combination of reduce + filter - Fifth (3.97 usec per loop)

      mquadri$ python -m timeit "l1 = [1,2,6,8]; l2 = [2,3,5,8];" "reduce(lambda x,y : filter(lambda z: z!=y,x) ,l1,l2)"
      100000 loops, best of 3: 3.97 usec per loop
      

    PS: set does not maintain the order and removes the duplicate elements from the list. Hence, do not use set difference if you need any of these.

提交回复
热议问题