Pythonic way of saying 'at least one of a, b, or c is in some_list'

Chris Kaynor ckaynor at zindagigames.com
Thu Oct 28 18:38:32 CEST 2010


On Thu, Oct 28, 2010 at 9:16 AM, cbrown at cbrownsystems.com <
cbrown at 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
>

A slightly cleaner form would be:

if any(day in days_off for day in ['Monday', 'Tuesday']):
    <block>

To make that a bit easier to read:
if any((day in days_off) for day in ['Monday', 'Tuesday']):
    <block>


>
> Is there a better pythonic idiom for this situation?
>
> Cheers - Chas
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20101028/fcb0d904/attachment.html>


More information about the Python-list mailing list