1

I have one tuple and one list:

t = (1, 2, 9)
l = range(2, 8)

and I want to check if there's at least one element of the list is in the tuple. I tried to use:

if (2 or 3 or 4 or 5 or 6 or 7) in l:
    return True

but it works only for the number 2.

Now I'm using:

if 2 in l or 3 in l or 4 in l or 5 in l or 6 in l or 7 in l:
    return True

but I think that's not the best way to do it. Is there a way to shrink this piece of code to make it more elegant?

Matteo Secco
  • 613
  • 4
  • 10
  • 19
  • 2
    Where are you interacting with `t` or any of its elements? Anyway, it's `any(item in l for item in t)` (or `any(item in t for item in l)`, if you wanted the opposite). – TigerhawkT3 Feb 27 '17 at 09:28

1 Answers1

5

You simply want:

any(e in t for e in l)
juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172