good way to do side effects on lists?

Michael Hoffman m.h.3.9.1.without.dots.at.cam.ac.uk at example.com
Thu Sep 23 01:32:16 CEST 2004


Danny Shevitz wrote:

> A minor stylistic question: Is there a better way to do:
> 
> for thing is myList:
>     myFunc(thing)

That seems like the best way to do it to me. It's very clear what it
means and any alternative techniques are of questionable benefit.

> map is close, but returns a list, which is wasted. I'm purely using side
> effects and the lists are large.

If you really wanted a one-liner

import itertools
class Dummy(object): pass

Dummy() in itertools.imap(myFunc, myList) # exhausts the iterable since your new Dummy instance will never be in the list

But with all that set-up it's not really a one-liner. And what's wrong with
your first proposal anyway?
--
Michael Hoffman



More information about the Python-list mailing list