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

Greg Ewing greg.ewing at canterbury.ac.nz
Thu Sep 27 04:09:16 CEST 2007


Terry Jones wrote:
> What's the most compact way to repeatedly call a function on a list without
> accumulating the results?

>     for x in mylist:
>         f(x)

That's it.

> 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?

No. And there would be no advantage in using LC syntax
even if it did. The generated bytecode would be essentially
identical.

If you really must have it all on one line, you can write

   for x in mylist: f(x)

-- 
Greg Ewing, Computer Science Dept, +--------------------------------------+
University of Canterbury,	   | Carpe post meridiem!          	  |
Christchurch, New Zealand	   | (I'm not a morning person.)          |
greg.ewing at canterbury.ac.nz	   +--------------------------------------+



More information about the Python-ideas mailing list