Python program to check if an element is present in list

In this tutorial, we will learn how to check whether the specified element is present in the list or not using multiple approaches in Python? By Shivang Yadav Last updated : August 17, 2023

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

List is a sequence data type. It is mutable as its values in the list can be modified. It is a collection of ordered sets of values enclosed in square brackets [].

Check if an element is present in the list

We will take a list as input from the user and then ask the user to enter the element's value to be searched. Then we will return YES/NO based on whether the element is present in the list or not.

Example

Input:
[4, 2, 9, 5, 1, 0, 7] element -> 5

Output:
Present

Python provides us multiple ways to perform the tasks. And to check for an element's presence also, we can use multiple ways in which the present can be check.

Method 1: Searching technique

To check whether an element is present in a list or not. We can simply use any searching technique and find the element in the list.

Here, we will discept linear search.

  • loop through the list
    • if : list[i] == element
    • found = true
    • break
  • if : found == true
    • print "found"
  • else : print "not found"

Program to check if an element is present in the list

# Python program to check if an 
# element exists in list 

# Getting list from user
myList = []
length = int(input("Enter number of elements: "))
for i in range(0, length):
    value = int(input())
    myList.append(value)
ele = int(input("Enter element to be searched in the list: "))

# checking for the presence of element in list
found = False
for i in myList:
    if(i == ele) :
        found = True
        break
if(found):
    print("Element found")
else :
    print("Element not found!")

The output of the above program is:

Enter number of elements: 5
2
6
8
4
1
Enter element to be searched in the list: 4
Element found

Method 2: Using in operator

Python programming language provides us an operator "in" to check whether an element is present in a list.

Syntax:

Returns boolean values based on the presence of element in the list.

Algorithm:

  • Get the list and seach_element from the user.
  • Check if the element is present in list using in
    • if seach_element in list
      • Print "element found"

Program to check if an element is present in a list using in operator

# Python program to check if an element 
# exists in list 

# Getting list from user
myList = []
length = int(input("Enter number of elements: "))
for i in range(0, length):
    value = int(input())
    myList.append(value)

ele = int(input("Enter element to be searched in the list: "))

# checking for the presence of element in list

if(ele in myList):
    print("Element found")
else :
    print("Element not found!")

The output of the above program is:

Enter number of elements: 5
10
20
30
40
50
Enter element to be searched in the list: 30
Element found

Method 3: Using bisect_left() method on sorted list

The list needs to be sorted in order to apply this method.

The bisect_left() method find's and returns the first occurrence of the given element in a sorted array.

Syntax:

For sorting list
    list_name.sort()
bisect_left method : 
		bisect_left(list, element)

Returns a boolean value based on whether the element is present in the list or not.

Program to check if an element is present in list using bisect_left() method on sorted list

# Python program to check if an element 
# exists in list 

import bisect

# Getting list from user
myList = []
length = int(input("Enter number of elements: "))
for i in range(0, length):
    value = int(input())
    myList.append(value)

ele = int(input("Enter element to be searched in the list: "))

# checking for the presence of element in list
myList.sort()
if(bisect.bisect_left(myList, ele) ):
    print("Element found")
else :
    print("Element not found!")

The output of the above program is:

Enter number of elements: 5
10
12
15
20
25
Enter element to be searched in the list: 25
Element found

Python List Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.