Fastest or most idiomatic way to remove object from list of objects in Python
Fastest or most idiomatic way to remove object from list of objects in Python
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.
Answer by Francesco for Fastest or most idiomatic way to remove object from list of objects in Python
What about the built in filter function?
>>> l = [1,2,3,4,5] >>> f = [4] >>> filter(lambda x: x not in f, l) [1, 2, 3, 5]
or in python3
>>> list(filter(lambda x: x not in f, l)) [1, 2, 3, 5]
Answer by piRSquared for Fastest or most idiomatic way to remove object from list of objects in Python
Use try
/except
wrapped in a recursive function recursion takes care of potential multiple M
's
def tremove(L, M): try: L.remove(M) return tremove(L, M) except: return L tremove(L, M)
Answer by Muhammad Tahir for Fastest or most idiomatic way to remove object from list of objects in Python
list.remove
seems to be the fastest way, with list comprehension as the second fastest and filter
at last.
Here are the timeit
results
In: python -m timeit '[x for x in [1,2,3,4,5] if x not in [4]]' Out: 1000000 loops, best of 3: 0.285 usec per loop In: python -m timeit '[x for x in [1,2,3,4,5] if x != 4]' Out: 1000000 loops, best of 3: 0.254 usec per loop In: python -m timeit 'filter(lambda x: x not in [4], [1,2,3,4,5])' Out: 1000000 loops, best of 3: 0.577 usec per loop In: python -m timeit 'filter(lambda x: x != 4, [1,2,3,4,5])' Out: 1000000 loops, best of 3: 0.569 usec per loop In: python -m timeit '[1,2,3,4,5].remove(4)' Out: 10000000 loops, best of 3: 0.132 usec per loop
Answer by MSeifert for Fastest or most idiomatic way to remove object from list of objects in Python
If there are multiple occurences of your value then probably a while
-loop with remove
is needed:
L = [1,2,3,4,5] while True: try: L.remove(4) except: break
It is a bit slower (due to the exception handling and multiple iterations over the list) than the list comprehension:
[ j for j in L if j != 4 ]
but both do work fine. If you want to exclude multiple values then you should use the list-comprehension:
M = [1, 4] [ j for j in L if j not in M ]
because the try
/ except
will be nested and the list comprehension only needs to traverse the list once.
Answer by timgeb for Fastest or most idiomatic way to remove object from list of objects in Python
Here's an idea that let's you make O(1) containment checks for hashable items. It should be dramatically faster for long lists M
with lots of hashables.
class MixedBag(object): def __init__(self, *args): self.hashed = set() self.nothashed = [] for x in args: self.add(x) def add(self, x): try: self.hashed.add(x) except TypeError: self.nothashed.append(x) def __contains__(self, x): try: return x in self.hashed except TypeError: return x in self.nothashed L = [[1,2,3], 4, '5', {6}] M = [[1,2,3], '5', {4}] mix = MixedBag(*M) L = [x for x in L if x not in mix] print(L) # [4, set([6])]
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment