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