Is there a short version of
n = [5, 3, 17]
if 5 in n and 17 in n:
print("YES")
something like that doesn't seem to work
if (5 and 17) in n:
print("YES")
Any suggestions?
Is there a short version of
n = [5, 3, 17]
if 5 in n and 17 in n:
print("YES")
something like that doesn't seem to work
if (5 and 17) in n:
print("YES")
Any suggestions?
I don't think there is something exactly like that in Python. The closest I can think of is, set operations, eg. set.issubset:
>>> n = [5, 3, 17]
>>> ({5, 17}).issubset(n)
True
You could use something like this instead:
n = [5,3,7]
if all(item in n for item in [5,7]):
print("YES")
n = [5, 3, 17]
if set(n) & set((5, 7)): # Using & which is intersect operator for two sets
print("YES")
Another way:
n = [5,3,17]
search = [5,17]
found = [x for x in search if x in n]
print("Count", len(found))
print("Matched", found)