a break for comprehensions
Kevin Lacker
kdl4 at acpub.duke.edu
Thu Jul 26 23:37:25 EDT 2001
> > Can you do this:
> > ...
> > with a list comprehension somehow? I'm starting to dislike explicit for
> > loops just for the purposes of constructing another list from a current
one.
>
> I can clearly understand what the above code is doing. Doing this
> as a list comprehension would be less readable for me (I'm not a
> heavy user of list comprehensions, yet.)
>
> Are you sure you really don't like the explicit, readable code
> above, and would prefer a more compressed, (admittedly explicit,
> I suppose), more cryptic version instead? There are other
> languages which could do the above in one tiny line, no doubt.
> (No names. :)
How cryptic is
[process(x) for x in my_list while is_good(x)]
? Not really any more cryptic than
[process(x) for x in my_list if is_good(x)]
which seems to be agreeably a good thing to have. I certainly like it.
I don't really know much Lisp, but in the back of my mind I half remember
someone claiming they had more flexible iteration abilities that still
looked functional and noncryptic...
More information about the Python-list
mailing list