Q:

Python program to check if an element is present in list

belongs to collection: Python List Programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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!")

Output:

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!")

Output:

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

# 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!")

Output:

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python List Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python program to swap any two elements in the lis... >>
<< How to find the length of a list in Python (3 effe...