0

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

Kevin
  • 74,910
  • 12
  • 133
  • 166
Milano
  • 18,048
  • 37
  • 153
  • 353

2 Answers2

2

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!
Veedrac
  • 58,273
  • 15
  • 112
  • 169
0

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
marceljg
  • 997
  • 4
  • 14