A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/bisect-algorithm-functions-in-python/ below:

Bisect Algorithm Functions in Python

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 ?
  1. Useful for binary search operations to search in a sorted list and to locate insertion points.
  2. Provides efficient methods to insert elements into a sorted list while maintaining order.
  3. Avoids the need for manual sorting after each insertion, saving time and effort.
  4. Offers functions like bisect(), bisect_left(), bisect_right() and insort() for clean optimized code.
  5. 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:

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:

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:

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:

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:


Bisect Algorithm Functions in Python


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4