
Dec. 23, 2021
6:53 p.m.
Chris Angelico wrote:
If you're removing multiple, it's usually best to filter. This is a great opportunity to learn about list comprehensions and the difference between O(n) and O(n²) :) ChrisA
It would be O(n) if done right. And could be much more efficient than a list comprehension, because: 1) The list's C code would do it. 2) It could stop comparing early. 3) It wouldn't have to touch the later elements (a list comp would, to increment ref counts). 4) Doesn't need to build a new list. Also, how would you write it as a list comprehension? Trivial for removing *all* occurrences, but for removing only up to some count? I can do it, but it's not pretty.