Pre-pep discussion material: in-place equivalents to map and filter

Terry Reedy tjreedy at udel.edu
Thu Nov 3 11:04:36 EDT 2016


On 11/3/2016 4:29 AM, Steven D'Aprano wrote:

> Nonsense. It is perfectly readable because it is explicit about what is being
> done, unlike some magic method that you have to read the docs to understand
> what it does.

Agreed.

> A list comprehension or for-loop is more general and can be combined so you can
> do both:
>
> alist[:] = [func(x) for x in alist if condition(x)]

The qualifier 'list' is not needed.  The right hand side of slice 
assignment just has to be an iterable.  So a second interation to build 
an intermediate list is not required.

alist[:] = (func(x) for x in alist if condition(x))

The parentheses around the generator expression are required here. 
(Steven, I know you know that, but not everyone else will.)

-- 
Terry Jan Reedy




More information about the Python-list mailing list