VOOZH about

URL: https://www.geeksforgeeks.org/python/binary-search-bisect-in-python/

⇱ Binary Search (bisect) in Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Binary Search (bisect) in Python

Last Updated : 11 Jul, 2025

Binary Search is a technique used to search element in a sorted list. In this article, we will looking at library functions to do Binary Search.
Finding first occurrence of an element. 
 

bisect.bisect_left(a, x, lo=0, hi=len(a)) : Returns leftmost insertion point of x in a sorted list. Last two parameters are optional, they are used to search in sublist.


 


Output: 
First occurrence of 4 is present at 2

 

Finding greatest value smaller than x. 
 


Output: 
Largest value smaller than 7 is at index 3

 

Finding rightmost occurrence
 

bisect.bisect_right(a, x, lo=0, hi=len(a)) Returns rightmost insertion point of x in a sorted list a. Last two parameters are optional, they are used to search in sublist.


 


Output: 
Last occurrence of 4 is present at 3

 

Please refer Binary Search for writing your own Binary Search code.
Reference : 
https://docs.python.org/3/library/bisect.html
 

Comment
Article Tags:
Article Tags: