The bisect module provides functions for working with sorted lists using binary search. It can quickly locate insertion positions and help maintain sorted order without manually searching through the list.
First Occurrence
When a sorted list contains duplicate values, it is often useful to find the position of the first occurrence of an element. The bisect_left() function returns the leftmost position where the value can be inserted while maintaining sorted order.
from bisect import bisect_left
arr = [1, 2, 4, 4, 8]
index = bisect_left(arr, 4)
if index < len(arr) and arr[index] == 4:
print("First occurrence of 4 is at index", index)
else:
print("Element not found")
Output
First occurrence of 4 is at index 2
Explanation:
- bisect_left(arr, 4) returns the leftmost insertion position for 4.
- Since 4 exists in the list, the returned index points to its first occurrence.
Largest Value Smaller Than a Target
Sometimes we need to find the position of the largest element that is smaller than a given value. bisect_left() can be used to quickly determine this position in a sorted list.
from bisect import bisect_left
arr = [1, 2, 4, 4, 8]
index = bisect_left(arr, 7)
if index > 0:
print("Largest value smaller than 7 is", arr[index - 1])
else:
print("No smaller value exists")
Output
Largest value smaller than 7 is 4
Explanation:
- bisect_left(arr, 7) returns the position where 7 should be inserted.
- The element immediately before that position is the largest value smaller than 7.
- If the returned index is 0, there is no smaller value in the list.
Last Occurrence
To find the last occurrence of a value in a sorted list containing duplicates, we can use bisect_right(). It returns the insertion position immediately after the rightmost occurrence of the element.
from bisect import bisect_right
arr = [1, 2, 4, 4, 8]
index = bisect_right(arr, 4)
if index > 0 and arr[index - 1] == 4:
print("Last occurrence of 4 is at index", index - 1)
else:
print("Element not found")
Output
Last occurrence of 4 is at index 3
Explanation:
- bisect_right(arr, 4) returns the insertion position after the last 4.
- Subtracting 1 gives the index of the rightmost occurrence.
- This method is useful when working with duplicate values in sorted data.
Inserting While Maintaining Sorted Order
Inserting elements into a sorted list without manually finding the correct position here, module automatically determines the insertion point and places the element while preserving the sorted order.
from bisect import insort
arr = [1, 3, 5, 7]
insort(arr, 4)
print(arr)
Output
[1, 3, 4, 5, 7]
Explanation:
- insort() finds the correct insertion position using binary search.
- The element 4 is inserted between 3 and 5.
- The list remains sorted after insertion.
Counting Occurrences of an Element
The positions returned by bisect_left() and bisect_right() can be used to count how many times an element appears in a sorted list.
from bisect import bisect_left, bisect_right
arr = [1, 2, 4, 4, 4, 8]
count = bisect_right(arr, 4) - bisect_left(arr, 4)
print(count)
Output
3
Explanation:
- bisect_left() finds the first position of 4.
- bisect_right() finds the position after the last 4.
- The difference between the two positions gives the total occurrence count.
Common bisect Functions
The bisect module provides a small set of functions for searching and inserting values into sorted lists.
| Function | Description |
|---|---|
| bisect_left() | Returns the leftmost insertion position |
| bisect_right() | Returns the rightmost insertion position |
| bisect() | Alias for bisect_right() |
| insort_left() | Inserts an element at the leftmost valid position |
| insort_right() | Inserts an element at the rightmost valid position |
| insort() | Alias for insort_right() |