Search code examples
pythonlistunit-testingassert

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


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?


Solution

  • Try using this:

    assert 210410 in x