Open In App

Bisect Algorithm Functions in Python

Last Updated : 02 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The bisect module in Python helps you efficiently maintain a sorted list by finding the correct position to insert an element, keeping the list sorted without needing to sort it again.

This is useful because it reduces the overhead of repeatedly sorting the list after each insertion, making your code faster and more efficient.

Key Functions in the Bisect Module

1. bisect()

This function returns the position where a number should be inserted to keep the list sorted. If the number already exists, it returns the rightmost insertion point.

Syntax:

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

Parameters:

  • list: The sorted list to work on
  • num: The number to insert
  • beg (optional): Starting index for search
  • end (optional): Ending index for search

2. bisect_left()

Returns the position where a number should be inserted to keep the list sorted. If the element exists, it returns the leftmost insertion point.

Syntax:

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

Parameters:

  • list: The sorted list to operate on
  • num: The number to insert
  • beg (optional): Starting index for search (default 0)
  • end (optional): Ending index for search (default length of list)

3. bisect_right()

Works similarly to bisect(), returning the rightmost insertion point.

Syntax:

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

Parameters:

  • list: The sorted list to operate on
  • num: The number to insert
  • beg (optional): Starting index for search (default 0)
  • end (optional): Ending index for search (default length of list)

Example:

Python
import bisect

li = [1, 3, 4, 4, 4, 6, 7]

print(bisect.bisect(li, 4))
print(bisect.bisect_left(li, 4))
print(bisect.bisect_right(li, 4, 0, 4))

Output
5
2
4

4. insort()

Inserts a number into the list at the rightmost appropriate position, keeping the list sorted. Unlike bisect methods which return the insertion index, insort methods insert the element directly into the list, updating it in place.

Syntax:

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

Parameters:

  • list: The sorted list to operate on
  • num: The number to insert
  • beg (optional): Starting index for insertion (default 0)
  • end (optional): Ending index for insertion (default length of list)

5. insort_left()

Works like insort(), but inserts the number at the leftmost appropriate position.

Syntax:

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

Parameters:

  • list: The sorted list to operate on
  • num: The number to insert
  • beg (optional): Starting index for insertion (default 0)
  • end (optional): Ending index for insertion (default length of list)

6. insort_right()

Works the same as insort(), inserting at the rightmost position within a given range.

Syntax:

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

Parameters:

  • list: The sorted list to operate on
  • num: The number to insert
  • beg (optional): Starting index for insertion (default 0)
  • end (optional): Ending index for insertion (default length of list)
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)
print(l1)

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

bisect.insort_right(l3, 5, 0, 4)
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]

Next Article
Practice Tags :

Similar Reads