What is Python bisect Module? How it Works?

What is bisect in Python?

Python bisect is a module under the bisect algorithm that enables programmers to maintain the list’s sort order after inserting each element.

This is important because it cuts down on the time it takes to sort the list after adding each item.

The goal of the Bisect algorithm is to figure out where to add the element in a list and to keep the list sort still in order.

List of different bisect functions

  • bisect(list, num, begin, end)
  • bisect_left(list, num, begin, end)
  • bisect_right(list, num, begin, end)
  • insort(list, num, begin, end)
  • insort_left(list, num, begin, end)
  • insort_right(list, num, begin, end)

Additionally, these function accepts four arguments:

  1. a list to be processed,
  2. a number to insert,
  3. the starting and ending positions in the list, and
  4. a number to insert.

Aside from this Python bisect module, you may also explore how to solve errors in modules through the Python No Module Named Error full tutorial.

Python bisect(list, num, beg, end)

The bisect (list, num, beg, end) function returns the position in the sorted list where the specified number can be inserted.

This method helps provide an output of a list that remains in sorted order.

However, if the element already exists in the list, the position on the right where it must be inserted is returned.

Example Program:

import bisect  
  
myList = [1, 2, 3, 4, 5, 5, 5, 5, 6, 7, 8, 9]  
  
print("The rightmost index to insert, so list remains sorted is: ", end = "")  

print(bisect.bisect(myList, 5))   

Output:

The rightmost index to insert, so list remains sorted is: 8

bisect_left(list, num, beg, end)

The bisect_left(list, num, beg, end) function is similar to bisect (list, num, beg, end).

But, if the inserted element already exists on the list, it returns the number of elements on the left position.

Let’s take a look at the example program below.

Example Program:

import bisect  
  
myList = [1, 2, 3, 4, 5, 5, 5, 5, 6, 7, 8, 9]  
  
print("The leftmost index to insert, so list remains sorted is: ", end = "")  

print(bisect.bisect_left(myList, 5))  

Output:

The leftmost index to insert, so list remains sorted is: 4

bisect_right(list, num, beg, end)

The Python bisect_right(list, num, beg, end) function operates similarly to the “bisect()” function.

The bisect_right function and the bisect(0 function have the same concept that if the element already exists in the list, the rightmost position where it must be inserted is returned.

Let’s clarify the statement with the example below:

Example Program:

import bisect  
  
myList = [1, 2, 3, 4, 5, 5, 5, 5, 6, 7, 8, 9]  
  
print("The right positioned index to insert, so list remains sorted is: ", end = "")  

print(bisect.bisect_right(myList, 5))  

Output:

The right positioned index to insert, so list remains sorted is: 8

Summary

In summary, the Python bisect module (Bisect Algorithm) has several functions covered.

These functions apply when inserting an object or value in a list without messing with the sorted list.

The bisect() and bisect_right() functions has similar application when implemented in programs.

These functions apply the insertion of value in the rightmost position of the list if that value already exists.

The bisect_left() function, on the other hand, apply the insertion of an element on the leftmost position of the list if the element already exists in the list.

Leave a Comment