[Python-ideas] Proposal how to remove all occurrences of a value from a Python list
M.-A. Lemburg
mal at egenix.com
Tue Oct 6 13:24:08 CEST 2015
If you have more than a few values to remove, it's often
faster to create a new list, since each removal will
require a copy operation of all trailing items and (every
now and then) a realloc of the list object to free
up the unused space:
new_arr = [x
for x in arr
if x != 1]
On 05.10.2015 22:18, Terry Reedy wrote:
> On 10/5/2015 2:28 AM, Sven R. Kunze wrote:
>> On 04.10.2015 01:32, Andrew Barnert via Python-ideas wrote:
>>> And now, everywhere you use it looks like this:
>>>
>>> remove_all(arr, 1)
>>>
>>> And it's hard to imagine anything more readable.
>>
>> arr.remove_all(1)
>>
>>> And, even if remove_all isn't the kind of function an experienced
>>> developer would write, learning how to factor out the tricky bits into
>>> documentable and testable functions is one of the most useful skills
>>> for any developer in any language.#
>>
>> True.
>>
>>
>> Btw. the same is true for Python core devs. This said, I would
>> appreciate the method 'remove_all' provided by the stdlib. ;-)
>
> The problem with methods is that they only work with one class. A list.removeall would only remove
> things equal to a specific item from a list (and presumably in place). We already have both a
> generic filter function and syntax that will remove all items from any iterable that meet any
> condition. The stream can be fed into any other function that accept an iterable.
--
Marc-Andre Lemburg
eGenix.com
Professional Python Services directly from the Source
>>> Python Projects, Coaching and Consulting ... http://www.egenix.com/
>>> mxODBC Plone/Zope Database Adapter ... http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
________________________________________________________________________
::::: Try our mxODBC.Connect Python Database Interface for free ! ::::::
eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
Registered at Amtsgericht Duesseldorf: HRB 46611
http://www.egenix.com/company/contact/
More information about the Python-ideas
mailing list