List Count

Blind Anagram blindanagram at nowhere.org
Tue Apr 23 16:41:03 EDT 2013


On 23/04/2013 21:00, Terry Jan Reedy wrote:
> On 4/23/2013 12:57 PM, Blind Anagram wrote:
> 
>> So, all I was doing in asking for advice was to check whether there is
>> an easy way of avoiding the slice copy,
> 
> And there is.
> 
>> not because this is critical,
>> but rather because it is a pity to limit the performance because Python
>> forces a (strictly unnecessary) copy in order to perform a count within
>> a part of a list.
>
> Python does not force that. You have been given several simple no-copy
> alternatives. They happen to be slower *with CPython* because of the
> speed difference between Python code and C code. If the same timing
> tests were done with any of the implementations that execute python code
> faster, the results would likely be different.

Then being pedantic rather than colloquial, the lack of start, end
parameters in the function list.count(value) means that anyone wishing
to use this function on a part of a list is forced to slice the list and
thereby invoke a possibly costly copy operation, one that is, in
principle, not necessary in order to perform the underlying operation.

[snip]
>> In other words, the lack of a list.count(value, limit) function makes
>> Python less effective than it would otherwise be.
> 
> Untrue. The alternatives are just as *effective*.

Then I fear that we will have to accept that we disagree on this.

   Brian




More information about the Python-list mailing list