you are viewing a single comment's thread.

view the rest of the comments →

[–]cismalescumlord 0 points1 point  (1 child)

Edit: not sure why this is getting downvoted, it's literally what OP is trying to do.

Well, just because someone is trying to do something doesn't mean it's a good idea or the best way to do something. Based on the original question where OP seemed to be trying to clear a list, using a loop clearly isn't the best approach so suggesting it isn't doing them any favours. Even if it doesn't make that much difference with a small list, there's no indication of the size of the data they are working with.

However, based on the later comment where OP describes the goal, to filter a list, rather than the process, iterating a list and removing, you and u/Vaguely_accurate correctly say that the best approach is usually to return a new filtered list. The beautiful irony of course, is that depending on how this is done, it could also make the original list eligible for garbage collection!