Assert to check if a element present in a list or not

Question:

I am trying to find if a particular element (int/string type), exists in my list or not. But I am using assert to evaluate my condition, meaning if the assert condition states True (element is present inside the list), False for element not being there in the list.

Here is what I am trying-

def test(x):
  try:
    for i in x:
      assert i==210410
      return True
  except AssertionError as msg:
    print('Error')


x=[210410,'ABC',21228,'YMCA',31334,'KJHG']

The output results to Error, even if the element is in the list. Can you please help me to sort this issue out?

Asked By: John

||

Answers:

Try using this:

assert 210410 in x
Answered By: Mohsen Dehghankar

assert 210410 not in x works as well

Answered By: Hakun
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.