Reputation: 18745
I have an if
condition in my code which seems for example like this:
a = [1,2,3]
b = [4,5,6]
c = [7,8,9]
I want 'If'
to return true when (1 is in a) or (1 is in b) or (1 is in c) or (5 is in a) or (5 is in b) or (5 is in c)
I've tried:
if (1 or 5) in (a or b or c):
pass
But this obviously didn't work that way. Could you give me a hint? Thanks
Upvotes: 0
Views: 73
Reputation: 1067
This seems to work for me, though there might be a built in to concatenate the three lists.
a = [1,2,3]
b = [4,5,6]
c = [7,8,9]
target = [1,5]
any(x for x in (a+b+c) if x in target)
True
Upvotes: 0
Reputation: 60167
You should probably use sets:
a = {1, 2, 3}
b = {4, 5, 6}
c = {7, 8, 9}
a | b | c
#>>> {1, 2, 3, 4, 5, 6, 7, 8, 9}
{1, 5} & (a | b | c)
#>>> {1, 5}
bool({1, 5} & (a | b | c))
#>>> True
if {1, 5} & (a | b | c):
print("Yeah!")
#>>> Yeah!
if not {1, 5}.isdisjoint(a | b | c):
print("Yeah!")
#>>> Yeah!
If you want short-circuiting:
if not all({1, 5}.isdisjoint(items) for items in (a, b, c)):
print("Yeah!")
#>>> Yeah!
Upvotes: 2