Home > OS >  Assert to check if a element present in a list or not
Assert to check if a element present in a list or not

Time:12-20

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?

CodePudding user response:

Try using this:

assert 210410 in x
  • Related