Op 30-08-13 09:25, Chris Angelico schreef: > On Fri, Aug 30, 2013 at 5:15 PM, Antoon Pardon > <antoon.par...@rece.vub.ac.be> wrote: >> Op 30-08-13 06:55, Ben Finney schreef: >>> Ben Finney <ben+pyt...@benfinney.id.au> writes: >>> >>>> Fábio Santos <fabiosantos...@gmail.com> writes: >>>> >>>>> It is a shame that this is not possible in python. for..if exists in >>>>> comprehensions and not in regular loops but that would be nice >>>>> sometimes. >>>> for foo in (spam for spam in sequence if predicate(spam)): … >>> >>> Better: >>> >>> for foo in filter(predicate, sequence): >>> process(foo) >> >> Well better in what way? You now have to translate a predicate >> expression into a predicate function. Which AFAIU was one of >> the reasons to move away from map/filter to list comprehension. >> >> As I understand it, python made a move away from map and filter >> towards list comprehension. Chris seems to want some of the >> possibilities that came with that incorporated into the for >> statement. And your suggestion is to go back to the old kind >> of filter way. > > No, actually Ben's quite right - assuming the predicate is a simple > function,
But why should we assume that? Suppose I would like to process all odd items in a list. A comprehension kind of notation would be | for item in lst if item % 2: | process items Which we would have to turn into | for item in filter(lambda nr: nr % 2, lst): | process items But AFAIR, one of the driving forces behind the introduction to list comprehension, and thus a move away from map and filter was to get rid of the lambda's in this kind of situations. > of course (Python's lambda notation is a bit clunky for > comparisons); as of Python 3, filter() is lazy and is pretty much what > I'm doing here. Lazy or not, is AFAICS, not a point here. -- Antoon Pardon -- http://mail.python.org/mailman/listinfo/python-list