Python Glossary Русский

all Python 3bisectfunction

bisect.insort_right()

insertion

Insert x into a sorted list after any equal entries, keeping it sorted; alias of insort(). The insertion is O(n) due to shifting.

Syntax

bisect.insort_right(a, x)

insort is just another name for this function, and it is the one you normally want: a new item lands after its equals, so items with the same key keep their arrival order. The linear shift is fine for hundreds or thousands of elements, but not for millions or a stream of frequent inserts — reach for heapq if you only need the minimum, or a dedicated sorted container.

Examples

import bisect
a = [1, 3, 5]
bisect.insort_right(a, 4)
a # → [1, 3, 4, 5]
bisect.insort_right(a, 0)
a # → [0, 1, 3, 4, 5]

See also

Open in the full glossarydocs.python.orgReport an inaccuracy