len() and __len__

Chris Liechti cliechti at gmx.net
Sun May 26 19:47:35 EDT 2002


Hans Nowak <wurmy at earthlink.net> wrote in 
news:3CF17033.72F7DB3F at earthlink.net:

> Chris Liechti wrote:
> 
>> but you can do
>> >>> listoflength = map(len, somelist)
>> 
>> which isn't that simple with __len__():
>> 
>> >>> listoflength = []
>> >>> for x in somelist:
>> ...     listoflength.append(x.__len__())
> 
> Or just
> 
>>>> map(lambda x: x.__len__(), ["foo", "bar", "bogus"])
> [3, 3, 5]

yes or a list comprehension...

>>> [x.__len__() for x in ["foo", "bar", "bogus"]] 
[3, 3, 5]

chris

-- 
Chris <cliechti at gmx.net>




More information about the Python-list mailing list