Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Bisect - Array bisection algorithm in Python
The bisect module provides efficient algorithms for maintaining sorted lists. Instead of sorting after every insertion, it uses binary search to find the correct position quickly. This is much more efficient for frequent insertions into large sorted lists.
bisect_left()
Finds the insertion point for a value to maintain sorted order. If the value already exists, it returns the position before any existing entries ?
import bisect
nums = [10, 20, 30, 40, 50]
position = bisect.bisect_left(nums, 25)
print(f"Insert 25 at position: {position}")
# Insert to verify
nums.insert(position, 25)
print(f"Updated list: {nums}")
Insert 25 at position: 2 Updated list: [10, 20, 25, 30, 40, 50]
bisect_right()
Similar to bisect_left(), but returns the position after any existing entries of the same value ?
import bisect
nums = [10, 20, 30, 30, 40, 50]
left_pos = bisect.bisect_left(nums, 30)
right_pos = bisect.bisect_right(nums, 30)
print(f"bisect_left(30): {left_pos}")
print(f"bisect_right(30): {right_pos}")
bisect_left(30): 2 bisect_right(30): 4
insort_left() and insort_right()
These methods combine finding the position and inserting in one operation ?
import bisect
nums = [10, 20, 30, 30, 40, 50]
print(f"Original list: {nums}")
# Insert using insort_left
bisect.insort_left(nums, 35)
print(f"After insort_left(35): {nums}")
# Insert using insort_right (same as insort)
bisect.insort_right(nums, 25)
print(f"After insort_right(25): {nums}")
Original list: [10, 20, 30, 30, 40, 50] After insort_left(35): [10, 20, 30, 30, 35, 40, 50] After insort_right(25): [10, 20, 25, 30, 30, 35, 40, 50]
Complete Example
Here's a practical example showing how bisect maintains sorted order efficiently ?
import bisect
# Start with unsorted data
numbers = [45, 21, 34, 87, 56, 12, 5, 98, 30, 63]
numbers.sort()
print(f"Sorted list: {numbers}")
# Find insertion position
position = bisect.bisect_left(numbers, 50)
print(f"Position to insert 50: {position}")
# Manual insertion
numbers.insert(position, 50)
print(f"After inserting 50: {numbers}")
# Automatic insertion with insort
bisect.insort(numbers, 29)
print(f"After insort(29): {numbers}")
Sorted list: [5, 12, 21, 30, 34, 45, 56, 63, 87, 98] Position to insert 50: 6 After inserting 50: [5, 12, 21, 30, 34, 45, 50, 56, 63, 87, 98] After insort(29): [5, 12, 21, 29, 30, 34, 45, 50, 56, 63, 87, 98]
Comparison
| Method | Action | Time Complexity |
|---|---|---|
bisect_left() |
Find position (before duplicates) | O(log n) |
bisect_right() |
Find position (after duplicates) | O(log n) |
insort_left() |
Insert before duplicates | O(n) |
insort_right() |
Insert after duplicates | O(n) |
Conclusion
The bisect module is ideal for maintaining sorted lists efficiently. Use bisect_left() or bisect_right() to find positions, and insort() methods for automatic insertion. This approach is much faster than sorting after every insertion.
