Open In App

Bisect Algorithm Functions in Python

Last Updated : 04 Jul, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

The bisect module helps you manage sorted lists by providing fast and easy-to-use functions to find the correct insertion points for new elements. This means you can insert items into a sorted list and preserve the order with minimal overhead.

Core Functions of the Bisect Module

The bisect module mainly offers two types of functionalities:

  • Finding the insertion point (without insertion)
  • Inserting elements at the correct position

1. Finding Insertion Points

These functions return the index where the new element should be inserted to keep the list sorted.

a) bisect.bisect(): Returns the rightmost insertion point for the element. If the element already exists, the insertion point will be after the existing entries.

bisect.bisect(list, num, beg=0, end=len(list))

  • list: Sorted list.
  • num: Element to insert.
  • beg: Start index for searching (optional).
  • end: End index for searching (optional).

b) bisect.bisect_left(): Returns the leftmost insertion point for the element. If the element exists, the insertion point will be before the existing entries.

bisect.bisect_left(list, num, beg=0, end=len(list))

  • list: Sorted list.
  • num: Element to insert.
  • beg: Start index for searching (optional).
  • end: End index for searching (optional).

c) bisect.bisect_right(): Identical to bisect.bisect(), returns the rightmost insertion point.

bisect.bisect_right(list, num, beg=0, end=len(list))

  • list: Sorted list.
  • num: Element to insert.
  • beg: Start index for searching (optional).
  • end: End index for searching (optional).

2. Inserting Elements

These functions insert the element at the proper position to maintain sorting.

a) bisect.insort(): Inserts the element at the rightmost position. Unlike bisect() functions, this actually modifies the list by inserting the element.

bisect.insort(list, num, beg=0, end=len(list))

  • list: Sorted list.
  • num: Element to insert.
  • beg (optional): Start index for insertion (default 0).
  • end (optional): End index for insertion (default len(list)).

b) bisect.insort_left(): Inserts the element at the leftmost position.

bisect.insort_left(list, num, beg=0, end=len(list))

  • list: Sorted list.
  • num: Element to insert.
  • beg (optional): Start index for insertion (default 0).
  • end (optional): End index for insertion (default len(list)).

c) bisect.insort_right(): Inserts the element at the rightmost position (similar to insort()).

bisect.insort_right(list, num, beg=0, end=len(list))

  • list: Sorted list.
  • num: Element to insert.
  • beg (optional): Start index for insertion (default 0).
  • end (optional): End index for insertion (default len(list)).

Examples

Example 1: Find insertion indices for the value 4 in a sorted list using different bisect functions.

Python
import bisect
li = [1, 3, 4, 4, 4, 6, 7] 

print(bisect.bisect(li, 4)) # right
print(bisect.bisect_left(li, 4)) # left
print(bisect.bisect_right(li, 4, 0, 4)) # subright

Output
5
2
4

Explanation:

  • bisect(li, 4): Returns 5 because it finds the rightmost position after the last 4 in the list (index 4), so the insertion point is 5.
  • bisect_left(li, 4): Returns 2 because it finds the leftmost position before the first 4 in the list (index 2).
  • bisect_right(li, 4, 0, 4): Works only on sublist li[0:4] and returns 4 because it inserts 4 after the last 4 in the sublist.

Example 2: Insert the value 5 into a sorted list while keeping it sorted, using different insertion strategies.

Python
import bisect
l1 = [1, 3, 4, 4, 4, 6, 7]  
l2 = [1, 3, 4, 4, 4, 6, 7]  
l3 = [1, 3, 4, 4, 4, 6, 7]  

bisect.insort(l1, 5) # right
print(l1) 

bisect.insort_left(l2, 5) # left
print(l2)

bisect.insort_right(l3, 5, 0, 4) # subright
print(l3)

Output
[1, 3, 4, 4, 4, 5, 6, 7]
[1, 3, 4, 4, 4, 5, 6, 7]
[1, 3, 4, 4, 5, 4, 6, 7]

Explanation:

  • insort(l1, 5) inserts 5 at the rightmost suitable position – after all 4s and before 6.
  • insort_left(l2, 5) inserts 5 at the leftmost suitable position – same as insort here since 5 isn't in the list.
  • insort_right(l3, 5, 0, 4) inserts 5 at index 4, working only on sublist l3[0:4] = [1, 3, 4, 4] after the last ≤ 5 in that range, without affecting the rest of the list.

Next Article
Practice Tags :

Similar Reads