How to check item exist in Python List

Python List

In this article, we are going to learn the ways How to check item exist in Python List with examples. We are going to learn how we can do this by search on value or condition.

List in python store data of different types includes int, string, list of list, boolean, tuples many more. Often there is a situation in data manipulation we have to search for an element in Python list. Here we will be going through different ways to find an item in Python list.

These are approaches used for check Python list contains a value.

1. ‘IN’ operator to Find element in Python list


Syntax

#the way in operator use

item in List

check item exists in integer Python list

In this code example, We have an integer list and we are taking input as the element that we have to find, by checking the element that exists in the list using the ‘in’ operator. Using in operator we can easily get the item in the list without using a loop.

For the element, we want to search we can assign it manually to a temp element after taking it from the user.

my_list = [4,16,17,32,40,15,18,19,67]


# take an element that we want to find from the list
element = int(input("Please enter the element you have to find :"))

if element in my_list:

  print("%s is exist in list" %(element))
else:

  print("%s is not exist in the list" %(element))

Output

Please enter the element you have to find:19
19 is exist in the list

check item exists in string Python list

my_list = ['dog','cat','mouse','ant','bee','fly','spider']


# take an element that we want to find from the list
element = input("Please enter element you have to find :")

if element in my_list:

  print("%s is exist in list" %(element))
else:

  print("%s is not exist in the list" %(element))

Output

Please enter the element you have to find: cat
cat is exist in list

2. Try/Catch Find an exist in Python List


Using the try/catch block we can Find an element in Python List. This is a faster way to check element exists in the list.

my_list = ['dog','cat','mouse','ant','bee','fly','spider']


ele_to_find = 'rat'

try:
  ele_Index = my_list.index(ele_to_find)

except ValueError:
  print("%s is not exist in list" %(ele_to_find))
else:

  print("%s is  exist in the list" %(ele_to_find))

Output

rat is not exist in list

3. Find an element in Python List using any() method


We can check the item exists in the list by using the any() method of Python. The any() method is used to check if any of the element of iterable objects(list, array, tuple, dictionary) returns true. If it returns true means it exists, if the iterable object is empty then it returns false.

my_list = ['dog','cat','mouse','ant','bee','fly','spider']


element = 'spider'

if any (element in item for item in my_list):
  print("%s is exist in list" %(element))
else:

  print("%s is not exist in the list" %(element))

Output

Please enter elemnt you have to find :spider
spider is exist in list

4. List.count() to Find element exist in Python List


The list. count() method will return the number of times the item exists in the list. If we found the count is greater than zero that means the item exists in the list else it does not exist.

Syntax

#syntax to use the list count method
list.count(element)

Let us understand with example

#program to find an element in the list

my_list = ['dog','cat','mouse','ant','bee','fly','spider']


# take an element that we want to find from the list
element = input("Please enter element you have to find :")

if my_list.count(element)>0:

  print("%s is exist in list" %(element))
else:

  print("%s is not exist in the list" %(element))

Output

Please enter element you have to find :dog
dog is exist in list

5. Check if an item exist in a list using custom method


In this code example, we are defining a custom function that takes two argument list and element exist in the list we are iterating each element of the list and comparing with search element if it is found we print list is containing the element else element does not exist in the list.

#defining a custom function to check element exist in list
def custfun_tofind_ele(list,element_to_search):
      for item in list:
       if item == element_to_search:
         return True
      return False
my_list = ['dog','cat','mouse','ant','bee','fly','spider','fat']
# take elemnt that we want to find from list
element = input("Please enter the element you have to find :")
#calling the custfun_tofind_ele 
if custfun_tofind_ele(my_list,element.lower()):
 print("%s is exist in list" %(element))
else:
   print("%s is not exist in list" %(element))

Output

Please enter the element you have to find: fat
fat is exist in list

6. Using list comprehension


In this code example, we are using the list comprehension to check the element exists in the list or not.

my_list = ['dog','cat','mouse','ant','bee','fly','spider']


# take elemnt that we want to find from list
element = input("Please enter elemnt you have to find :")
matches =[item for item in my_list if element in item]

if matches:
  print("%s is exist in list" %(element))
else:

  print("%s is not exist in the list" %(element))

Output

Please enter the element you have to find: lizard
lizard is not exist in the list

Conclusion:

We hope after going through this post of How to check item exist in Python List with examples.you will be able to easily find the element from the list using the above approaches.