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

cbrown at cbrownsystems.com cbrown at cbrownsystems.com
Sun Oct 31 21:38:21 EDT 2010


On Oct 31, 4:27 pm, Lawrence D'Oliveiro <l... at geek-
central.gen.new_zealand> wrote:
> In message
> <687bcb76-0093-4d68-ba56-0390a3e1e... at 30g2000yql.googlegroups.com>,
>
> cbr... at cbrownsystems.com wrote:
> > I should note that efficiency is not an issue to me here; this is for
> > when you have, say, a list user_options of at most around 15 options
> > or so, and you want to perform some action if one or more of a, b, or
> > c is an option listed in user_options. Big O notation isn't so
> > relevant as readability and 'naturalness'.
>
> I must say this whole thread has turned into a massive bikeshedding
> exercise.

Agreed that, to paraphrase RWE, a foolish insistence on big O
valuation is the hobgoblin of etc. On the other hand, I got a
reasonable answer to my question:

    if any(option in ['optionA', 'optionB', 'optionC']
            for option in supplied_options):
        first_do_something_those_options_all_require()
        if 'optionA' in supplied_options:
            do_optionA_specific_stuff()
        <and so on and so forth...>

which is quite readable and flexible as an idiom, and so I am
satisfied overall.

Cheers - Chas



More information about the Python-list mailing list