In this article we will discuss different ways to check if a given element exists in list or not.

Suppose we have a list of strings i.e.

Now let’s check if given list contains a string element ‘at’ ,

Check if element exists in list using python “in” Operator

Condition to check if element is in List :

It will return True, if element exists in list else return false.

For example check if ‘at’ exists in list i.e.

Condition to check if element is not in List :

Check if element exist in list using list.count() function

count(element) function returns the occurrence count of given element in the list. If its greater than 0, it means given element exists in list.

Check if element exist in list based on custom logic

Python any() function checks if any Element of given Iterable is True.

Let’s use it to check if any string element in list is of length 5 i.e.

Instead of condition we can use separate function in any to match the condition i.e.

Complete example is as follows,



Python Recommendations:

C++ & C++11 Recommendations:

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Subscribe with us to join 1500+ Python & C++ developers, to get more Tips &  Tutorials like this.