Calling a function of a list without accumulating results

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. Terry

On 9/26/07, Terry Jones <terry@jon.es> wrote:
Only after the list is completely constructed. List comprehensions are literally 'for' loops with an append call to a method so without extending the peepholer to notice this case and strip out the list creation and appending it is not optimized.
Go with the 'for' loop as Adam suggested. I just don't see this as needing syntax support. -Brett

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.
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@canterbury.ac.nz +--------------------------------------+

On Wed, September 26, 2007 10:42 pm, Terry Jones wrote:
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) Done! -- Arnaud

On Sep 27, 2007, at 1:02 PM, Arnaud Delobelle wrote:
Ooooh... I like this! Anyone who needs such a construct can just write their own exhaust() function, too, since I see no reason to pollute the distribution with this. -Fred -- Fred Drake <fdrake at acm.org>

On 9/26/07, Terry Jones <terry@jon.es> wrote:
Only after the list is completely constructed. List comprehensions are literally 'for' loops with an append call to a method so without extending the peepholer to notice this case and strip out the list creation and appending it is not optimized.
Go with the 'for' loop as Adam suggested. I just don't see this as needing syntax support. -Brett

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.
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@canterbury.ac.nz +--------------------------------------+

On Wed, September 26, 2007 10:42 pm, Terry Jones wrote:
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) Done! -- Arnaud

On Sep 27, 2007, at 1:02 PM, Arnaud Delobelle wrote:
Ooooh... I like this! Anyone who needs such a construct can just write their own exhaust() function, too, since I see no reason to pollute the distribution with this. -Fred -- Fred Drake <fdrake at acm.org>
participants (7)
-
Adam Olsen
-
Arnaud Delobelle
-
Brett Cannon
-
Fred Drake
-
Greg Ewing
-
Lucio Torre
-
Terry Jones