Python bisect module

The bisect module is a part of the Python Standard Library that provides support for efficient searching and insertion operations on ordered lists. It is implemented using the bisection algorithm, which is a divide-and-conquer strategy that repeatedly splits a list in half until the target element is found or it is clear that the element is not present in the list. The bisection algorithm is very efficient, with a time complexity of O(log n) for both search and insertion operations, where n is the size of the list. This makes it much faster than linear search algorithms, which have a time complexity of O(n).

The bisect module provides two main functions for searching for the position at which a given element should be inserted in an ordered list to maintain its sorted order: bisect_left and bisect_right. The bisect_left function returns the position of the first occurrence of the element in the list, while the bisect_right function returns the position of the last occurrence of the element in the list. These functions take a sorted list and a target element as arguments and return an integer indicating the position at which the element should be inserted to maintain the sorted order of the list.

In addition to these two main functions, the bisect module also provides two helper functions for inserting an element into a sorted list: insort_left and insort_right. These functions take a sorted list and an element as arguments and insert the element into the list at the position returned by bisect_left or bisect_right, respectively. These functions are useful for maintaining the sorted order of a list as new elements are added to it.

Here is an example of how to use the bisect module to search for and insert an element into an ordered list:

Copy codeimport bisect

# Create an ordered list
lst = [1, 2, 3, 4, 5]

# Search for the position of 4 in the list
pos = bisect.bisect_left(lst, 4)
print(pos)  # Output: 3

# Insert the element 6 into the list at the correct position
bisect.insort_left(lst, 6)
print(lst)  # Output: [1, 2, 3, 4, 5, 6]

In the above example, the bisect_left function returns the position of the first occurrence of element 4 in the list, which is 3. The insort_left function then inserts element 6 into the list at this position, maintaining the sorted order of the list.

The bisect module also provides two additional functions: bisect_key and insort_key. These functions work in a similar way to bisect_left, bisect_right, insort_left, and insort_right, but they allow you to specify a key function that is used to extract a comparison key from each element in the list. This can be useful if you want to use a different criterion for sorting the list than the elements themselves.

For example, consider the following code snippet:

import bisect

# Create a list of tuples
lst = [(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e')]

# Define a key function that returns the second element of each tuple
def key_func(x):
  return x[1]

# Search for the position of the tuple (4, 'd') in the list
pos = bisect.bisect_left(lst, (4, 'd'), key=key_func)

The bisect module provides the following functions:

  • bisect_left(lst, elem, lo=0, hi=len(lst)): This function searches for the position at which the element elem should be inserted in the sorted list lst to maintain its sorted order. It returns the position of the first occurrence of elem in the list. The optional arguments lo and hi can be used to specify the range of indices within the list to be searched. The default values of lo and hi are 0 and len(lst), respectively, which means that the entire list will be searched.

  • bisect_right(lst, elem, lo=0, hi=len(lst)): This function is similar to bisect_left, but it returns the position of the last occurrence of elem in the list.

  • insort_left(lst, elem, lo=0, hi=len(lst)): This function inserts the element elem into the sorted list lst at the position returned by bisect_left. It returns None. The optional arguments lo and hi can be used to specify the range of indices within the list to be searched. The default values of lo and hi are 0 and len(lst), respectively, which means that the entire list will be searched.

  • insort_right(lst, elem, lo=0, hi=len(lst)): This function is similar to insort_left, but it inserts the element at the position returned by bisect_right.

  • bisect_key(lst, elem, key=None, lo=0, hi=len(lst)): This function is similar to bisect_left, but it allows you to specify a key function that is used to extract a comparison key from each element in the list. The key function should take an element as input and return a value that can be used for comparison. The key function is applied to each element in the list before the element is compared to elem.

  • insort_key(lst, elem, key=None, lo=0, hi=len(lst)): This function is similar to insort_left, but it allows you to specify a key function that is used to extract a comparison key from each element in the list. The key function should take an element as input and return a value that can be used for comparison. The key function is applied to each element in the list before the element is compared to elem.

Here are some resources that you may find helpful for learning more about the bisect module in Python:

Did you find this article valuable?

Support Tech Blog by Soam by becoming a sponsor. Any amount is appreciated!