Search code examples
pythonpython-itertools

fast deletion in a list with too many elements - PYTHON


How can I quickly delete elements in b from inside a ?

import itertools

a=list(itertools.product("12X",repeat=15))
b=list(itertools.product("1X",repeat=15))
print("a len = ",len(a))
print("b len = ",len(b))

for x in b:
    if x in a:
        a.remove(x)

It's very slow.


Solution

  • Convert the lists to sets and just subtract.

    import itertools
    
    a=list(itertools.product("123",repeat=3))
    b=list(itertools.product("12",repeat=3))
    print("a len = ",len(a))
    print("b len = ",len(b))
    c = set(a) - set(b)
    print(len(c))
    
    # This is the equivalent of
    c = set(a).difference(set(b))
    
    >>> a len =  27
    >>> b len =  8
    >>> 19
    

    You can also do set(a).intersection(set(b)) in order to return those that appear in both lists.

    If order matters, just use a list comprehension:

    excluded_elems = set(b)
    c = [x for x in a if x not in excluded_elems]
    print(len(c))
    

    Make sure to compute set(b) once, outside the comprehension, instead of inlining it in the not in check, which would compute it on every iteration.