Bisect Algorithm Functions in Python
Last Updated :
01 Aug, 2025
The bisect module in Python provides simple and fast (binary search based) functions to search an element in a sorted list, find the correct position to insert new elements, and insert new elements ensuring that the list remains sorted.
Why do we need Bisect Module ?
- Useful for binary search operations to search in a sorted list and to locate insertion points.
- Provides efficient methods to insert elements into a sorted list while maintaining order.
- Avoids the need for manual sorting after each insertion, saving time and effort.
- Offers functions like bisect(), bisect_left(), bisect_right() and insort() for clean optimized code.
- Ideal for tasks like maintaining leaderboards, ranked data or any scenario involving sorted data insertion/search.
Core Functions of 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))
Parameter:
- 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))
c) bisect.bisect_right(): Identical to bisect.bisect(), returns the rightmost insertion point.
bisect.bisect_right(list, num, beg=0, end=len(list))
Example: 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
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.
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))
Parameter:
- 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))
c) bisect.insort_right(): Inserts the element at the rightmost position (similar to insort()).
bisect.insort_right(list, num, beg=0, end=len(list))
Example: 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.
Bisect Algorithm Functions in Python
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice