Signup/Sign In

Python Program to check if element exists in list

In this tutorial, we will learn how to check if an element is present in a list or not. List is an ordered set of values enclosed in square brackets [ ]. List stores some values called elements in it, which can be accessed by their particular index. We will be discussing the various approaches by which we can check for an element in the given list.

The program will accept the list and the element which has to check, it should return True if it exists else, it will return False

Input: [6, 22, 7, 3, 0, 11] element= 0

Output: True

Let us look at the different approaches we can follow to execute the task.

Approach To Find Element

In this tutorial, we will be discussing the three main methods for checking if an element exists in a list.

  1. Comparing each value in the list to the element.
  2. Using in operator to check for an element in the list.
  3. Using count() to count the number of times an element is present.

Let us look at each approach separately

Approach 1: comparing each element

In this approach, we will compare each value present in the list with the element which has to be checked for its presence in the list. Simply, run a loop for all the elements in the list and compare every element in the list.

Algorithm

Follow the algorithm to understand the approach better

Step 1- Define a function that accepts list and the element as parameters

Step 2- Run a loop for all elements in the list

Step 3- Check for every iteration if the element is present in the list

Step 4- Return True if found

Step 5- Else, return False

Step 6- Initialise list and element

Step 7- Pass it into function and print the result.

Python Program 1

Look at the program to understand the implementation of the above-mentioned approach.

# find an element in list

#function
def check(list,element):
    for i in list:
         if i==element:
             return True  
    return False

#initialise list
li=[9, 2, 34, 2, 5, 9, 16]
element= 34

print("Check if",element,"is in",li)
print(check(li,element))


Check if 34 is in [9, 2, 34, 2, 5, 9, 16]
True

Approach 2: Find using in operator

In this approach, we will use the in operator to check if the element is present in the list or not and display the result accordingly. This particular way returns True if an element exists in the list and False if the element does not exist in the list. With this method, we can execute the above-mentioned condition in one line without using the loop.

Algorithm

Follow the algorithm to understand the approach better

Step 1- Define a function check() with list and element as parameters

Step 2- Use in operator to check if the element is present in the list

Step 3- Return True if found

Step 4- Return False if not found

Step 5- Initialise the list with the element

Step 6- Pass in the function and print the result

Python Program 2

Look at the program to understand the implementation of the above-mentioned approach

#using in

def check(list,element):
    if element in list:
        return True
    else:
        return False


#initialise list
li=[9, 2, 34, 2, 5, 9, 16]
element= 9

print("Check if",element,"is in",li)
print(check(li,element))


Check if 9 is in [9, 2, 34, 2, 5, 9, 16]
True

Approach 3: Using count() Function

In this approach, we will use the count() function. It is a built-in Python list function that checks if the passed element exists in the list. If the passed element exists in the list, count() method will return the number of times it occurs in the entire list. If it returns a non-zero positive number, it means an element exists in the given list.

Algorithm

Follow the algorithm to understand the approach better

Step 1- Define a function check() with list and element as parameters

Step 2- Call list.count() and store the value returned by it in a variable c

Step 3- If c>0 then return True

Step 4- Else, return False

Step 5- Initialise list

Step 6-Pass the list and element as parameters

Step 7- Display the result

Python Program 3

Look at the program to understand the implementation of the above-mentioned approach.

#using count() 

def check(list,element):
    c=list.count(element)
    if c>0:
        return True
    else:
        return False


#initialise list
li=[9, 2, 34, 2, 5, 9, 16]
element= 20

print("Check if",element,"is in",li)
print(check(li,element))


Check if 20 is in [9, 2, 34, 2, 5, 9, 16]
False

Conclusion

So far, we have discussed three approaches for checking if an element is present in a list or not. We have discussed the naive approach that is to compare each element in the list. This method is not optimal for a list with many values as the execution time of the program will increase. We can use the two other methods which were discussed, by using in operator and count() function to find if an element exists in the list.



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.