Find a value in a list
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Searching for a value within a list is a common task in both everyday computing and in the field of computer science. Depending on the type of list and the specific requirements of the search, different methods can be employed. Below, we will discuss various techniques including linear search and binary search, along with Python code examples to illustrate these methods in practice.
Linear Search
Linear search, also known as sequential search, is the simplest search technique. It involves iterating through each element in the list until the desired value is found or the end of the list is reached.
Example of Linear Search:
Binary Search
Binary search is a more efficient method but requires that the list be sorted in advance. This method repeatedly divides the list in half, eliminating the half that cannot contain the target each time, until the target is found or the sublists are exhausted.
Example of Binary Search:
Complexity Considerations
The efficiency of search operations is generally assessed based on their time complexity:
- Linear Search: Has a time complexity of
O(n), wherenis the number of elements in the list. It is less efficient on larger lists but doesn't require the list to be sorted. - Binary Search: Offers a time complexity of
O(log n), significantly faster especially for large datasets but necessitates the list being in sorted order.
Comparison Table
| Search Method | Best Case Time Complexity | Average Case Time Complexity | Worst Case Time Complexity | Requires Sorted List |
| Linear Search | No | |||
| Binary Search | Yes |
Enhanced Search Techniques
Beyond these basic methods, various algorithms and data structures are designed to optimize search operations further, including:
- Hash Tables: Offering average-case constant time complexity, , for search operations.
- Binary Search Trees: Particularly in scenarios where data needs frequent updates alongside the search operations.
Conclusion
The method chosen for finding a value in a list can significantly affect the efficiency of your application, particularly as data scales. Linear search serves basic unsorted data needs well, while binary search provides a significant performance boost for searching in sorted lists. For applications requiring the utmost efficiency, more advanced structures like hash tables or binary search trees might be appropriate. Choose the right search technique considering the structure of your data and the operations you need to perform most frequently.
Related reading
- Find a value in a list
- Find all chordless cycles in an undirected graph
- Find all pairs of integers within an array which sum to a specified value
- Find all paths between two graph nodes
- Find all permutations of 2 arrays in JS
- Find all possible combinations of a String representation of a number
- Find all subsets of length k in an array
- Find all the paths forming simple cycles on an undirected graph

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.