On 04/07/10 00:16, Albert van der Horst wrote: > In article <houv8a$ed9$0...@news.t-online.com>, > Peter Otten <__pete...@web.de> wrote: >> Pierre Quentel wrote: >> >>> I'm surprised nobody proposed a solution with itertools ;-) >> >> next(itertools.takewhile(lambda _: a == b, ["yes"]), "no") > > I could learn something here, if you explain it?
The signature for next() is: next(iterator[, default]) In particular, pay attention the `default` parameter. next() returns `default` if StopIteration is raised else it returns iterator.__next__(). takewhile(predicate, ["yes"]).__next__() return the string "yes" if predicate("yes") returns True else it raises StopIteration. The predicate (lambda _: a == b) returns True if (a == b) is True otherwise it returns False. Put the next(), takewhile(), and the predicate together and you get an a monster that returns `default` if a == b is False else "yes". -- http://mail.python.org/mailman/listinfo/python-list