Suppose I have a list L of unknown objects, O1 to On, and I want to remove another object reference M which may refer to one of the objects in L, I've managed to do it using:
L = [ O1, O2, ... On]
...
L = [ j for j in L if j not in [ M ] ]
which is lovely and idiomatic... but I'm having to do it a lot, and I wonder if there's not another more idiomatic way, or if there is not a faster way.
The important point is that the list of objects is unknown, and may or may not include the object to be excluded. I want to avoid extending or subclassing the objects where possible.
Mis a single object why would you want to doj not in [M]?j == Mwill definitely be a little faster as direct comparisons are always faster.try:L.remove(M) except ValueError:pass? However theremovemethod only removes the first element equal toM.