# How to Do a Binary Search in Python

DevFeed: [How to Do a Binary Search in Python](<https://devfeed.tech/articles/how-to-do-a-binary-search-in-python-134046.md>)

Original publisher: [Read original article](<https://realpython.com/binary-search-python/>)

Author: Bartosz Zaczyński

Published: 2026-11-14T14:00:00Z

Content type: tutorial

Language: en

Sources: [Real Python](<https://devfeed.tech/sources/real-python.md>)

Topics: [Python](<https://devfeed.tech/topics/python.md>), [Tutorial](<https://devfeed.tech/topics/tutorial.md>), [Learning](<https://devfeed.tech/topics/learning.md>), [floating-point](<https://devfeed.tech/topics/floating-point.md>)

Tags: [algorithm](<https://devfeed.tech/tags/algorithm.md>), [binary-search](<https://devfeed.tech/tags/binary-search.md>), [implement](<https://devfeed.tech/tags/implement.md>), [module](<https://devfeed.tech/tags/module.md>), [python](<https://devfeed.tech/tags/python.md>), [search](<https://devfeed.tech/tags/search.md>)

## AI overview

Binary search can locate values in a sorted list with logarithmic comparisons by repeatedly halving the remaining range; the excerpt estimates at most about 20 comparisons for one million items. Python's `bisect` module finds insertion positions and maintains sorted lists, but the list must already be sorted. For duplicates, `bisect_left()` and `bisect_right()` expose the matching range; searches by key can use precomputed keys. The tutorial also builds iterative and recursive implementations, discussing boundary updates, missing values, duplicate results, slicing overhead in recursion, and floating-point comparisons. It recommends using library code unless implementation is needed for learning or constraints. Hash-based `dict` lookup can be faster and independent of item position, but the described IMDb benchmark uses about 0.5 GB more memory and requires keeping the index consistent; dictionary keys must be hashable. The excerpt includes benchmark context and complexity discussion, but does not provide the full tutorial.

## Source excerpt

Use the bisect module to do a binary search in Python, then implement this classic algorithm yourself, both iteratively and recursively.