Say I have different sets (they have to be different, I cannot join them as per the kind of data I am working with):

```
r = set([1,2,3])
s = set([4,5,6])
t = set([7,8,9])
```

What is the best way to check if a given variable is present in any of them?

I am using:

```
if myvar in r \
or myvar in s \
or myvar in t:
```

But I wonder if this can be reduced somehow by using `set`

's properties such as `union`

.

The following works, but I can't find a way to define multiple unions:

```
if myvar in r.union(s)
or myvar in t:
```

I am also wondering if this union will somehow affect performance, since I guess a temporary `set`

will be created on the fly.

## Best Solution

Just use any:

set lookups are

`0(1)`

so creating a union to check if the variable is in any set is totally unnecessary instead of simply checking using`in`

with`any`

which will short circuit as soon as a match is found and does not create a new set.And I am also wondering if this union will affect somehow performanceYes of course unioning the sets affects performance, it adds to the complexity, you are creating a new set every time which is

`O(len(r)+len(s)+len(t))`

so you can say goodbye to the real point of using sets which are efficient lookups.So the bottom line is that is you want to keep efficient lookups you will have to union the set once and keep them in memory creating a new variable then using that to do your lookup for

`myvar`

so the initial creation will be`0(n)`

and lookups will be`0(1)`

thereafter.If you don't every time you want to do a lookup first creating the union you will have a linear solution in the length of

`r+s+t -> set.union(*(r, s, t))`

as opposed to at worst three constant(on average) lookups. That also means always adding or removing any elements from the new unioned set that are added/removed from`r,s`

or`t`

.Some realistic timings on moderately large sized sets show exactly the difference:

Timing the union shows that pretty much all the time is spent in the union calls:

Using larger sets and getting the element in the last set:

There is literally no difference no matter how large the sets get using

`any`

but as the set sizes grow so does the running time using union.The only way to make it faster would be to stick to

`or`

but we are taking the difference of a few hundred nanoseconds which is the cost of creating the generator expression and the function call:To union sets set.union(*(r, s, t)) is also the fastest as you don't build intermediary sets: