Chris Angelico writes: > On Wed, Mar 23, 2016 at 12:15 AM, Jussi Piitulainen wrote: >> Chris Angelico writes: >> >>> Or use filter(), which is sometimes clearer: >>> >>> # You probably want a more sophisticated function here >>> def nonspace(ch): return not ch.isspace() >>> >>> next(filter(nonspace, stream)) >> >> Sure. >> >> # But there's more fun hiding in the standard library. >> next(itertools.filterfalse(operator.methodcaller('isspace'), stream)) > > ... at that point, the genexp is miles ahead in readability :)
;) > Although I do sometimes yearn for a "filterout" function that does the > same thing as filter() but negates its predicate. Then you could use: > > next(filterout(str.isspace, stream)) > > to say "give me the next from the stream, filtering out those which > are spaces". It's not hard to write, of course. from itertools import filterfalse as filterout next(filterout(str.isspace, """ I didn't know str.isspace works like that! """)) ---> 'I' -- https://mail.python.org/mailman/listinfo/python-list