[Python-ideas] Re: Add a `count` argument to `list.remove`

2021-12-26 Thread Stefan Pochmann
Steven D'Aprano wrote: > Can you sketch an O(n) algorithm for removing multiple items from an > array, which *doesn't* involving building a temporary new list? Here's what I meant. Have read/write indexes, delete the gap after maxcount occurrences: def remove(lst, value, maxcount): read =

[Python-ideas] Re: Add a `count` argument to `list.remove`

2021-12-25 Thread Jeremiah Vivian
Chris Angelico wrote: > On Sun, Dec 26, 2021 at 11:32 AM Jeremiah Vivian > nohackingofkrow...@gmail.com wrote: > > Steven D'Aprano wrote: > > On Thu, Dec 23, 2021 at 05:53:46PM -, Stefan Pochmann wrote: > > Chris Angelico wrote: > > If you're removing multiple, it's usually best to filter.

[Python-ideas] Re: Add a `count` argument to `list.remove`

2021-12-25 Thread Chris Angelico
On Sun, Dec 26, 2021 at 11:32 AM Jeremiah Vivian wrote: > > Steven D'Aprano wrote: > > On Thu, Dec 23, 2021 at 05:53:46PM -, Stefan Pochmann wrote: > > > Chris Angelico wrote: > > > If you're removing multiple, it's usually best to filter. This is a > > > great opportunity to learn about list

[Python-ideas] Re: Add a `count` argument to `list.remove`

2021-12-25 Thread Jeremiah Vivian
Steven D'Aprano wrote: > On Thu, Dec 23, 2021 at 05:53:46PM -, Stefan Pochmann wrote: > > 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²) :) > >

[Python-ideas] Re: Add a `count` argument to `list.remove`

2021-12-25 Thread Chris Angelico
On Sun, Dec 26, 2021 at 11:00 AM Steven D'Aprano wrote: > > On Thu, Dec 23, 2021 at 05:53:46PM -, Stefan Pochmann wrote: > > 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 > > >

[Python-ideas] Re: Add a `count` argument to `list.remove`

2021-12-25 Thread MRAB
On 2021-12-25 23:52, Steven D'Aprano wrote: On Thu, Dec 23, 2021 at 05:53:46PM -, Stefan Pochmann wrote: 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

[Python-ideas] Re: Add a `count` argument to `list.remove`

2021-12-25 Thread Steven D'Aprano
On Thu, Dec 23, 2021 at 05:53:46PM -, Stefan Pochmann wrote: > 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

[Python-ideas] Re: Add a `count` argument to `list.remove`

2021-12-24 Thread Stefan Pochmann
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,

[Python-ideas] Re: Add a `count` argument to `list.remove`

2021-12-21 Thread Jeremiah Vivian
> When called with the count argument, should it raise ValueError if there > are fewer than `count` occurrences of the element in the list? No, it shouldn't. It would raise ValueError if the element is not found within the list though. ___ Python-ideas

[Python-ideas] Re: Add a `count` argument to `list.remove`

2021-12-21 Thread MRAB
On 2021-12-22 01:53, Rob Cliffe via Python-ideas wrote: Currently list.remove raises ValueError if the element is not in the list. When called with the count argument, should it raise ValueError if there are fewer than `count` occurrences of the element in the list? There's str.replace and

[Python-ideas] Re: Add a `count` argument to `list.remove`

2021-12-21 Thread Rob Cliffe via Python-ideas
Currently list.remove raises ValueError if the element is not in the list. When called with the count argument, should it raise ValueError if there are fewer than `count` occurrences of the element in the list? Best wishes Rob Cliffe On 22/12/2021 00:09, Jeremiah Vivian wrote: I expect some

[Python-ideas] Re: Add a `count` argument to `list.remove`

2021-12-21 Thread Chris Angelico
On Wed, Dec 22, 2021 at 11:12 AM Jeremiah Vivian wrote: > > I expect some sort of "there's no benefit in this, just write the current > implementation", indirectly or directly. > Currently, this is the way to remove `num` occurrences of an element from a > list: > > idx = 0 > > while idx <