Moshanator
Moshanator

Reputation: 286

How to look for a single value in a set of tuples?

Suppose I have this set:

a = set([(1,2),(3,4),(5,6)])

How do I test if "3" or "7" can be found in that set?

Upvotes: 0

Views: 119

Answers (4)

eumiro
eumiro

Reputation: 212835

a = set([(1,2),(3,4),(5,6)])
b = set((3,7))
any(b&set(p) for p in a)
# True

@RikPoggi also proposed using isdisjoint, which works even without creating a set:

any(not b.isdisjoint(p) for p in a)

Upvotes: 2

Rusty Rob
Rusty Rob

Reputation: 17173

>>> from itertools import chain
>>> if set(chain(*set([(1,2),(3,4),(5,6)])))&set([3,7]):
...     print True
...     
True

Upvotes: 0

Rik Poggi
Rik Poggi

Reputation: 29302

With any():

any(3 in t or 7 in t for t in a)

Upvotes: 4

Wesley
Wesley

Reputation: 2264

for tuple in a:
    for value in tuple:
        if value in [3, 7]:
            print 'found'

Upvotes: 1

Related Questions