On Oct 28, 12:33 pm, "cbr...@cbrownsystems.com" <cbr...@cbrownsystems.com> wrote: > On Oct 28, 9:23 am, John Posner <jjpos...@optimum.net> wrote: > > > > > On 10/28/2010 12:16 PM, cbr...@cbrownsystems.com wrote: > > > > It's clear but tedious to write: > > > > if 'monday" in days_off or "tuesday" in days_off: > > > doSomething > > > > I currently am tending to write: > > > > if any([d for d in ['monday', 'tuesday'] if d in days_off]): > > > doSomething > > > > Is there a better pythonic idiom for this situation? > > > Clunky, but it might prompt you to think of a better idea: convert the > > lists to sets, and take their intersection. > > > -John > > I thought of that as well, e.g.: > > if set(["monday,"tuesday']).intersection(set(days_off)): > doSomething > > but those extra recasts to set() make it unaesthetic to me; and worse > > if not set(["monday,"tuesday']).isdisjoint(set(days_off)): > doSomething > > is bound to confuse most readers. > > Cheers - Chjas
If you have Python 2.7 or newer you don't need to recast: if {"monday", "tuesday"}.intersection(days_off): doSomething -- http://mail.python.org/mailman/listinfo/python-list