Skip to content
Home » Python program for linear search in linear list

Python program for linear search in linear list

Linear list: A linear data structure is that whose elements form a sequence. When elements of linear data structures are homogeneous and are represented in memory by means of sequential memory locations, these are called arrays.

Linear search: In linear search, each element of the array/linear list is compared with the given item to be searched for, one by one.

l=eval(input("Enter a set of numbers in the list:"))
print("Elements of the list:",l)
num=eval(input("Enter the element to be searched:"))
flag=False
p=len(l)
for i in range(p):
    if(num==l[i]):
        flag=True
        break
if(flag==True):
    print("Number is present in the list")
    print("Search is successful.")
else:
    print("Number is not present in the list")
    print("Search is unsuccessful.")

Output 1:

Enter a set of numbers in the list:[13,15,17,18,23,25,26]
Elements of the list: [13, 15, 17, 18, 23, 25, 26]
Enter the element to be searched:14
Number is not present in the list
Search is unsuccessful.

Output 2:

Enter a set of numbers in the list:[13,15,17,18,23,25,26,7]
Elements of the list: [13, 15, 17, 18, 23, 25, 26, 7]
Enter the element to be searched:7
Number is present in the list
Search is successful.