Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

8
  • 2
    @Scrontch Given you additional criteria of "modify the list in place", this looks like the cleanest solution. As some have mentioned, you don't want to iterate over the same list you're modifying. Commented May 16, 2011 at 20:41
  • 10
    Ok, but it seems not to be performing well at all. Won't this be O(n^2)? (And that is not counting the initial list copy). Removing the element on the fly would be O(n). Commented May 16, 2011 at 20:52
  • 4
    using map (or list comprehensions) for side effects and throwing away the result is not very pythonic Commented May 16, 2011 at 21:40
  • 3
    The alternative implementation is wrong. You will need to reverse 'toremove' before the map function. Otherwise later indices point to wrong objects. Commented Nov 22, 2012 at 11:51
  • 14
    This seems pretty broken, list.remove removes the first occurrence of a value by equality, if you were trying to remove all float values from [0, 1, 1.0, 0] just by doing .remove(0.0) etc. you would end up with [1.0, 0.0] which is definitely not the result with all float removed. Commented Oct 30, 2016 at 23:58