A bit weird dictionary behavior
steve at REMOVE-THIS-cybersource.com.au
Tue Sep 23 01:23:54 CEST 2008
On Mon, 22 Sep 2008 07:35:50 -0700, bearophileHUGS wrote:
> Tino Wildenhain:
>> len([x for x in iterable if x==y])
>> or even shorter:
>> not work and read better anyway?
> The first version creates an actual list just to take its length, think
> about how much memory it may use.
For many iterables, the amount of memory is not excessive and the
increase in readability of len() is to be preferred over the side-effect
of sum(1 for...).
But sure, in general you shouldn't try to count the number of items in an
arbitrary iterable unless you know how much time and resources it will
end up using. That's why I don't think len() should support arbitrary
>> even calculating with boolean values isn't neccessary since 'and' and
>> 'foo if bar else blub' are working much better so the type coalescing
>> bool - int - float can really go away.
> I don't understand.
I think Tino means that you don't need to cast items to bool since you
can use ints, floats etc. directly as truth values.
More information about the Python-list