Sequential Search using Python

The Sequential search algorithm is a searching algorithm. To implement this algorithm, we start by searching for the target value from the beginning of the array and continue till we find the target value. In this article, I will take you through the implementation of Sequential Search using Python.

Sequential Search Algorithm

The searching algorithms are the algorithms that are used to search a particular value in a data structure such as lists in Python. The sequential search is a searching algorithm that checks each item in a data structure from the beginning to find the target value.

For example, imagine that you are trying to find a specific card from a deck of cards. You will go through each card in the deck one by one until you find the card you are looking for. Once you got the card you were looking for you will stop. This is how the sequential search algorithm works. Now, in the section below, I will take you through an implementation of sequential search using the Python programming language.

Sequential Search using Python

I hope you now know what a sequential search algorithm is and how it works. To implement this algorithm, we need to check each element from the beginning until we find the value we are looking for. Now let’s see how to implement sequential search using Python:

In the Python code above, I have defined a Python function with two parameters(list_, n) the “list_” indicates the Python list and “n” indicates the item we want to search in a list. Then I am simply using a for loop to search for n in the list. Now let’s see how to use the above function for implementing the sequential search algorithm:

numbers = list(range(0, 20))
print(sequential_search(numbers, 3))

Also, Read – Python Projects with Source Code: Solved and Explained.

Summary

The sequential search is one of those algorithms which are popular in every coding interview. To implement it, you have to search for the target value from the beginning of the data structure until you find the target value. I hope you liked this article on the implementation of Sequential Search using Python. Feel free to ask your valuable questions in the comments section below.

Default image
Aman Kharwal
Coder with the ♥️ of a Writer || Data Scientist | Solopreneur | Founder
Articles: 1054

6 Comments

  1. HI SIR. I HAVE IMPLEMENTED THIS CODE IN JUPYTER NOTEBOOK. I AM JUST GETTING “TRUE” as Output for whichever value i am giving. I didn’t understand this?. If I am giving n value greater than 20, even then I am getting True as Output.

    • Yes, it returns true if it finds the element in the specified range.

      • Hi Sir, I have same idea as Alugumetra.
        This argorithm might have a little problem. I implemented this code in jupyternotebook but it doesn’t work well.

        def sequential_search(list_, n):
        for i in list_:
        if i == n:
        break
        return True

        elements = range(0, 20)
        ss = sequential_search(elements, 60)
        print(ss)

        I expected output is error or something else, but result was just True.
        The value I wanted was 60, and my specified range is 0 to 19, so value is over range and couldn’t find but return True.
        Could you tell us solution.
        Thanks in advance.

      • Thanks for notifying about the error, I have updated the code, it will work now

  2. Tq u sir, for u r valuable time to spread knowledge.

Leave a Reply