When data is stored in a list, you will often need to search through the list to see if a specific value is stored there. This article covers a template for handling this task — the Search a List template. At the end of the article I’ll show you how to use a built-in Python function to perform this task without writing your own function.

The Search a List Template

The Search a List template implements a sequential, or linear, search of a list. This means that the search starts at the first element of the list and ends when either the searched-for element is found or the search reaches the end of the list.

I am going to implement this template with a function. One version of the template will return a true value if the search is successful and it will return a false value if the search fails. In the second version, the function will return the position of the found value if the search is successful and the function will return -1 if the search fails. The reason a failed search returns -1 is that is not a valid list position.

#python-list #programming #python-programming #python

Learning Python: Search a List
1.75 GEEK