[Python-ideas] Calling a function of a list without accumulating results

Arnaud Delobelle arno at marooned.org.uk
Thu Sep 27 19:02:28 CEST 2007

On Wed, September 26, 2007 10:42 pm, Terry Jones wrote:
> What's the most compact way to repeatedly call a function on a list
> without
> accumulating the results?
> While I can accumulate results via
>     a = [f(x) for x in mylist]
> or with a generator, there doesn't seem to be a way to do this without
> accumulating the results. I guess I need to either use the above and
> ignore
> the result, or use
>     for x in mylist:
>         f(x)
> I run into this need quite frequently. If I write
>     [f(x) for x in mylist]
> with no assignment, will Python notice that I don't want the accumulated
> results and silently toss them for me?
> A possible syntax change would be to allow the unadorned
>     f(x) for x in mylist
> And raise an error if someone tries to assign to this.

If you want to do it like this, why not do it explicitly:

def exhaust(iterable):
   for i in iterable: pass

Then you can write:

exhaust(f(x) for x in mylist)



More information about the Python-ideas mailing list