In this article we are going to find a way to search an element from a list without using the 'in' method. A list is a data structure in Python that is changeable, ordered sequence of elements. Each element or value which is inside the list is called as item. Lists are defined by having values between square brackets '[ ]'.
In our program we first create a list with different items in it. After that we ask the user to input the item name to be searched from the list using the input() method. In the same line after input() we use lower() method so that even if the user gives input in uppercase letters our program won't throw a garbage result.
We will now create a function 'Search' to search the item given by the user. To search the item we will iterate through each element in the list and check whether item is equal to the element. If element at any index matches our item then our program will print "item found at index x." where item will be replaced by the item name given by user and x will be replaced by the index number at which we found our item. Else our program will print "item is not at index x."where item will be replaced by item name and x will be replaced by index number. In the end we'll call our function and pass the values.
breakFast = ['bread','butter','jam']
element = input("Enter the element to be searched: ").lower()
for i in range(len(breakFast)):
if alist[i] == elem:
print(elem + " found at index " + str(i))
print(elem + " is not at index " + str(i))