Friday, October 3, 2025
HomeLanguagesPython | Linear search on list or tuples

Python | Linear search on list or tuples

Let us see a basic linear search operation on Python lists and tuples. A simple approach is to do a linear search, that is 

  • Start from the leftmost element of the list and one by one compare x with each element of the list.
  • If x matches with an element, return True.
  • If x doesn’t match with any of the elements, return False.

 Example #1: Linear Search on Lists 

Python




# Search function with parameter list name
# and the value to be searched
  
  
def search(List, n):
  
    for i in range(len(List)):
        if List[i] == n:
            return True
    return False
  
  
# list which contains both string and numbers.
List = [1, 2, 'sachin', 4, 'Geeks', 6]
  
# Driver Code
n = 'Geeks'
  
if search(List, n):
    print("Found")
else:
    print("Not Found")


Output:

Found

Note: that lists are mutable but tuples are not. 

Example #2: Linear Search in Tuple 

Python




# Search function with parameter tuple name
# and the value to be searched
  
  
def search(Tuple, n):
  
    for i in range(len(Tuple)):
        if Tuple[i] == n:
            return True
    return False
  
  
# Tuple which contains both string and numbers.
Tuple = (1, 2, 'sachin', 4, 'Geeks', 6)
  
  
# Driver Code
n = 'Geeks'
  
if search(Tuple, n):
    print("Found")
else:
    print("Not Found")


Output:

Found
RELATED ARTICLES

Most Popular

Dominic
32331 POSTS0 COMMENTS
Milvus
85 POSTS0 COMMENTS
Nango Kala
6703 POSTS0 COMMENTS
Nicole Veronica
11867 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11929 POSTS0 COMMENTS
Shaida Kate Naidoo
6818 POSTS0 COMMENTS
Ted Musemwa
7080 POSTS0 COMMENTS
Thapelo Manthata
6775 POSTS0 COMMENTS
Umr Jansen
6776 POSTS0 COMMENTS