Program to find the given element in list using Python

Introduction

In this section, we will understand the python code to find whether the element is in the list or not. Python provides ‘in’ keyword to easily find the element in the list.

Program to find the given element in list

Program

list = ['is', 'st', 'around', 'in', 'the']
element = input("Enter the preposition to find: ")
# Check if it exists in list
if (element in list):
    print("Element Exists!")
else:
    print("Element does not exists!")

 

Output

Program to find the given element in list Output

Explanation

In the above python code, we have created a variable element which takes the user input. Through ‘in’ statement the variable element is checked against each value in list and if the searched value exists in the list the output “Element exists!” is displayed on the screen otherwise “Element does not exists!” is displayed.

The other way to find the element in the list is by converting the list into set first and then applying ‘in’ to the set. The disadvantage of using set() + in is that the duplicate value will get removed.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.