Just wondering

Peter Otten __peter__ at web.de
Fri May 15 09:03:33 EDT 2009


Jaime Fernandez del Rio wrote:

>> (1) building another throwaway list and
>> (2) function call overhead for calling doit()
>>
>> You can avoid (1) by using filter() instead of map()
> 
> Are you sure of this? 

> I'm afraid there is no builtin function to do an inplace modification
> of a sequence...
 
You are right. 

But the OP invokes map() for the side effect of calling doit(item) for every 
item in the (huge) list and then gets another list of the same length 
containing only None values. by using filter() instead of map() with doit 
always returning None he gets an empty result list which means less wasted 
effort:

>>> items = range(10)
>>> map(doit, items)
[None, None, None, None, None, None, None, None, None, None]
>>> filter(doit, items)
[]

Peter




More information about the Python-list mailing list