[Tutor] using in over several entities

Kent Johnson kent37 at tds.net
Fri Aug 24 17:20:02 CEST 2007

Tino Dai wrote:
> Hi there,
>     I am wondering about a short cut to doing this. Let's say that we 
> have an array:
> dbs= ['oracle','mysql','postgres','infomix','access']
> and we wanted to do this:
> if 'oracle' in dbs or 'mysql' in dbs or 'bdb' in dbs:
>    <... do something ...>
> Is there a short way or writing this? Something like
>     ('oracle','mysql','bdb') in dbs

I don't think there is a shortcut with lists. You could write
any(db in dbs for db in ('oracle','mysql','bdb'))

which at least lets you use a list for the targets, or
if [ db for db in ('oracle','mysql','bdb') if db in dbs ]:

With sets you can write
dbs= set(['oracle','mysql','postgres','infomix','access'])
if dbs.intersection(('oracle','mysql','bdb')):


More information about the Tutor mailing list