◐ Shell
clean mode source ↗

Message 377464 - Python tracker

> The usually recommended alternative is to make a new list of 
> things not deleted.  But one can do this in-place by writing
> the new list on top of the old by using a explicit second 
> index to move items just once.

I don't think we should send users down this path.  Better to stick with high level, easy-to-implement and performant suggestions:

"""
To edit a list in-place it is often simplest and fastest to replace the entire list:

   colors[:] = [color for color in colors if websafe(color)]

This makes a single pass through the list and efficiently builds a new list.  The colors[:] then replaces the entire contents of the original list with the new list
"""

Besides being easy to get right, this is likely to be *much* faster than tracking two indicies to manipulate the array in-place.  Slice operations run at the speed of a C memcpy (plus the ref count changes) and don't involve creating and destroying integer objects for indexing.