{"id":3416,"date":"2020-06-28T13:25:33","date_gmt":"2020-06-28T07:55:33","guid":{"rendered":"http:\/\/www.pythonpool.com\/?p=3416"},"modified":"2026-07-13T12:27:01","modified_gmt":"2026-07-13T06:57:01","slug":"python-bisect","status":"publish","type":"post","link":"https:\/\/www.pythonpool.com\/python-bisect\/","title":{"rendered":"Python bisect: Search and Insert in Sorted Lists"},"content":{"rendered":"<p><strong>Quick answer:<\/strong> bisect searches a sorted sequence for an insertion point without sorting it on every operation. bisect_left places a new value before equal entries, while bisect_right places it after them. The search is logarithmic, but list.insert() still shifts later elements, so the data structure choice matters for frequent updates.<\/p>\n<figure class=\"pythonpool-article-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-bisect.png\" alt=\"Python Pool infographic showing bisect left bisect right insertion point and sorted list invariants\" width=\"1536\" height=\"1024\" loading=\"lazy\" decoding=\"async\"><figcaption>bisect searches a sorted list for an insertion point; bisect_left and bisect_right make duplicate handling explicit.<\/figcaption><\/figure>\n<p>Python&#8217;s <code>bisect<\/code> module finds insertion points in sorted lists. It is useful when a program needs to search an ordered sequence or add new items while keeping that order intact. bisect works on Python sequences; <a href=\"https:\/\/www.pythonpool.com\/numpy-searchsorted\/\">NumPy searchsorted Guide for Sorted Arrays<\/a> applies the same insertion-point idea to NumPy arrays and vectorized query values.<\/p>\n<p>The official Python documentation for <a href=\"https:\/\/docs.python.org\/3\/library\/bisect.html\">bisect<\/a> is the main reference for these functions. The Python <a href=\"https:\/\/docs.python.org\/3\/howto\/sorting.html\">Sorting HOWTO<\/a> explains sort keys and ordering behavior, and the <a href=\"https:\/\/docs.python.org\/3\/library\/stdtypes.html#lists\">list documentation<\/a> covers the list operations used below.<\/p>\n<p>Use <code>bisect<\/code> when the data is already sorted and the program needs positions. The module does not check that the list is sorted for you. If the input order is wrong, the returned position can also be wrong.<\/p>\n<p>The two search functions are <code>bisect_left()<\/code> and <code>bisect_right()<\/code>. The left form returns the position before equal items. The right form returns the position after equal items. The shorter name <code>bisect()<\/code> is an alias for <code>bisect_right()<\/code>.<\/p>\n<p>The insert helpers are <code>insort_left()<\/code> and <code>insort_right()<\/code>. They first find the correct insertion point, then insert the item into the list. This keeps the list ordered, but insertion into the middle of a list still has to move later items.<\/p>\n<p>That tradeoff matters. Searching with bisect is efficient, but inserting into a Python list can still cost time proportional to the list length. For a few updates or medium-sized ordered lists, the simple list approach is often excellent. For very heavy update workloads, a different data structure may fit better. bisect searches a sorted sequence, while <a href=\"https:\/\/www.pythonpool.com\/efficiently-organize-your-data-with-python-trie\/\">Python Trie Data Structure: Prefix Search Guide<\/a> is the better comparison when the keys are strings and prefix lookup matters.<\/p>\n<p>Think of <code>bisect<\/code> as a tool for ordered positions, not as a general search replacement. It shines when sorted order is part of the design: grade boundaries, time slots, thresholds, duplicate ranges, leaderboards, cut points, and ordered lookup tables.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #990303;color:#990303\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #990303;color:#990303\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.pythonpool.com\/python-bisect\/#Find_Insertion_Points\" >Find Insertion Points<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.pythonpool.com\/python-bisect\/#Check_Whether_A_Value_Exists\" >Check Whether A Value Exists<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.pythonpool.com\/python-bisect\/#Keep_A_List_Sorted_With_insort\" >Keep A List Sorted With insort<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.pythonpool.com\/python-bisect\/#Count_Values_In_A_Range\" >Count Values In A Range<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.pythonpool.com\/python-bisect\/#Insert_Ordered_Records_With_Tuples\" >Insert Ordered Records With Tuples<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.pythonpool.com\/python-bisect\/#Maintain_A_Running_Sorted_Sample\" >Maintain A Running Sorted Sample<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.pythonpool.com\/python-bisect\/#Find_An_Insertion_Point\" >Find An Insertion Point<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.pythonpool.com\/python-bisect\/#Insert_While_Preserving_Duplicates\" >Insert While Preserving Duplicates<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.pythonpool.com\/python-bisect\/#Use_Bounds_And_Validate_Invariants\" >Use Bounds And Validate Invariants<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.pythonpool.com\/python-bisect\/#Know_The_Performance_Tradeoff\" >Know The Performance Tradeoff<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.pythonpool.com\/python-bisect\/#Frequently_Asked_Questions\" >Frequently Asked Questions<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.pythonpool.com\/python-bisect\/#What_is_Python_bisect_used_for\" >What is Python bisect used for?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/www.pythonpool.com\/python-bisect\/#What_is_the_difference_between_bisect_left_and_bisect_right\" >What is the difference between bisect_left and bisect_right?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/www.pythonpool.com\/python-bisect\/#Does_bisect_sort_a_list\" >Does bisect sort a list?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/www.pythonpool.com\/python-bisect\/#Is_bisect_faster_than_searching_a_list\" >Is bisect faster than searching a list?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"Find_Insertion_Points\"><\/span>Find Insertion Points<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Use <code>bisect_left()<\/code> and <code>bisect_right()<\/code> to see where a target would fit in a sorted list.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">import bisect\n\nnumbers = [10, 20, 20, 30, 40]\n\nprint(bisect.bisect_left(numbers, 20))\nprint(bisect.bisect_right(numbers, 20))\nprint(bisect.bisect(numbers, 20))\n<\/code><\/pre>\n<\/div>\n<p>The left position points before the first matching <code>20<\/code>. The right position points after the last matching <code>20<\/code>.<\/p>\n<p>This difference is important when duplicates are allowed. It lets you choose whether a new item should go before or after existing equal items.<\/p>\n<p>If the target is not present, both functions return the same insertion point. That position can still be useful because it tells you where the target belongs.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Check_Whether_A_Value_Exists\"><\/span>Check Whether A Value Exists<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><code>bisect_left()<\/code> can support an existence check by looking at the item at the returned position.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">import bisect\n\ndef index_of(sorted_values, target):\n    position = bisect.bisect_left(sorted_values, target)\n    if position != len(sorted_values) and sorted_values[position] == target:\n        return position\n    return None\n\nnumbers = [4, 8, 15, 16, 23, 42]\n\nprint(index_of(numbers, 15))\nprint(index_of(numbers, 9))\n<\/code><\/pre>\n<\/div>\n<p>The function returns an index only when the target is actually present. Otherwise it returns <code>None<\/code>.<\/p>\n<p>This pattern is safer than assuming the insertion point is a match. The insertion point might be the place where the target would be inserted, not a position containing the target.<\/p>\n<p>Use this style when you need a clear distinction between found and not found.<\/p>\n<p><!-- Python Pool visual layout repair 2026-07-13 --><\/p>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/bisect-sorted-b182.png\" alt=\"Python Pool infographic showing a sorted Python list, target value, and insertion point\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>Sorted list: A sorted Python list, target value, and insertion point.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Keep_A_List_Sorted_With_insort\"><\/span>Keep A List Sorted With insort<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Use <code>insort()<\/code> or <code>insort_right()<\/code> to insert after equal items. Use <code>insort_left()<\/code> to insert before equal items.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">import bisect\n\nscores = [70, 80, 90]\n\nbisect.insort(scores, 85)\nbisect.insort_left(scores, 70)\n\nprint(scores)\n<\/code><\/pre>\n<\/div>\n<p>The list remains sorted after each insert.<\/p>\n<p>This is convenient for ordered collections that receive occasional new items. It avoids calling <code>sort()<\/code> after every append.<\/p>\n<p>Remember that the insertion step changes the list in place. If other code holds the same list, it will see the updated order too.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Count_Values_In_A_Range\"><\/span>Count Values In A Range<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Two bisect calls can count how many sorted values fall inside a range.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">import bisect\n\ndef count_between(sorted_values, low, high):\n    left = bisect.bisect_left(sorted_values, low)\n    right = bisect.bisect_right(sorted_values, high)\n    return right - left\n\ngrades = [55, 70, 75, 80, 85, 90, 98]\n\nprint(count_between(grades, 70, 89))\n<\/code><\/pre>\n<\/div>\n<p>The left boundary includes values equal to <code>low<\/code>. The right boundary includes values equal to <code>high<\/code>.<\/p>\n<p>This technique is useful for buckets, score bands, timestamps, and threshold reports. Because it uses positions, it avoids scanning every item in the list.<\/p>\n<p>Define the range rules carefully. If the upper boundary should be exclusive, use <code>bisect_left()<\/code> for the high side instead.<\/p>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/bisect-search-b182.png\" alt=\"Python Pool infographic comparing bisect_left, bisect_right, duplicates, and boundaries\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>Find position: Bisect_left, bisect_right, duplicates, and boundaries.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Insert_Ordered_Records_With_Tuples\"><\/span>Insert Ordered Records With Tuples<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Tuples are compared from left to right, so they work well when the first item is the ordering field.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">import bisect\n\nevents = [\n    (10, \"draft\"),\n    (20, \"review\"),\n    (30, \"ship\"),\n]\n\nposition = bisect.bisect_left(events, (25, \"test\"))\nevents.insert(position, (25, \"test\"))\n\nprint(events)\n<\/code><\/pre>\n<\/div>\n<p>The new event is placed between the entries with order values <code>20<\/code> and <code>30<\/code>.<\/p>\n<p>This tuple approach keeps the example simple because the ordering value travels with the label. For larger records, keep the sort key consistent across every item.<\/p>\n<p>If two records can share the same ordering value, decide whether new records should go to the left or right of matching records. That choice affects stable presentation.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Maintain_A_Running_Sorted_Sample\"><\/span>Maintain A Running Sorted Sample<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><code>insort()<\/code> can maintain a sorted list while items arrive one at a time.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">from bisect import insort\n\ndef median(sorted_values):\n    middle = len(sorted_values) \/\/ 2\n    if len(sorted_values) % 2:\n        return sorted_values[middle]\n    return (sorted_values[middle - 1] + sorted_values[middle]) \/ 2\n\nsorted_values = []\nfor item in [5, 1, 9, 3]:\n    insort(sorted_values, item)\n\nprint(sorted_values)\nprint(median(sorted_values))\n<\/code><\/pre>\n<\/div>\n<p>The list is sorted after each arrival, so the median function can read from the middle positions directly.<\/p>\n<p>This works well for small streams, examples, and cases where readable code matters more than specialized performance. For very large streams, measure the cost of middle insertions before choosing this approach.<\/p>\n<p>In short, use <code>bisect_left()<\/code> when the left side of duplicates matters, <code>bisect_right()<\/code> when the right side matters, and <code>insort()<\/code> when a sorted list should stay ordered after insertion. Keep the input sorted, choose duplicate behavior deliberately, and remember that list insertion still moves items.<\/p>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/bisect-insert-b182.png\" alt=\"Python Pool infographic mapping bisect insertion into a sorted list while preserving order\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>Insert value: Bisect insertion into a sorted list while preserving order.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Find_An_Insertion_Point\"><\/span>Find An Insertion Point<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The bisect functions return an integer position, not the matching value. Keep the input sorted according to the same ordering used by the search. If the list is sorted by a derived key, use the key parameter where supported or maintain a parallel key list.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">from bisect import bisect_left, bisect_right\n\nscores = [10, 20, 20, 40]\nprint(bisect_left(scores, 20))\nprint(bisect_right(scores, 20))\nprint(bisect_left(scores, 25))<\/code><\/pre>\n<\/div>\n<h2><span class=\"ez-toc-section\" id=\"Insert_While_Preserving_Duplicates\"><\/span>Insert While Preserving Duplicates<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>insort_left() and insort_right() combine a search with list insertion. Choose the side based on whether a new equal value should precede or follow existing entries. The result is sorted only if the list was sorted before the call and all values use a compatible ordering.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">from bisect import insort_left, insort_right\n\nleft = [1, 2, 2, 4]\nright = left.copy()\ninsort_left(left, 2)\ninsort_right(right, 2)\nprint(left)\nprint(right)<\/code><\/pre>\n<\/div>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/bisect-check-b182.png\" alt=\"Python Pool infographic testing key functions, unsorted input, performance, and validation\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>Bisect checks: Key functions, unsorted input, performance, and validation.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Use_Bounds_And_Validate_Invariants\"><\/span>Use Bounds And Validate Invariants<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>lo and hi restrict a search to a slice of the list. Bounds are useful for a sorted window, but they do not check that values outside the window are sorted. Add tests for empty lists, duplicates, values below and above the range, and the invariant after every insertion policy.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">from bisect import bisect_left\n\nvalues = [10, 20, 30, 40, 50]\nposition = bisect_left(values, 35, lo=1, hi=4)\nprint(position)\nassert values == sorted(values)<\/code><\/pre>\n<\/div>\n<h2><span class=\"ez-toc-section\" id=\"Know_The_Performance_Tradeoff\"><\/span>Know The Performance Tradeoff<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The binary search is efficient, but inserting into a Python list is not a tree operation: elements after the insertion point move. For many updates, consider a different sorted collection or batch values and sort once. For occasional lookups and inserts, bisect is simple and dependable.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">from bisect import bisect_left\n\nvalues = []\nfor value in [30, 10, 20]:\n    values.insert(bisect_left(values, value), value)\nprint(values)<\/code><\/pre>\n<\/div>\n<p>The Python <a href=\"https:\/\/docs.python.org\/3\/library\/bisect.html\">bisect module documentation<\/a> defines left and right insertion points, bounded searches, and the insertion performance note. Keep the sorted-list invariant explicit in the code that owns the collection.<\/p>\n<p>For related ordered collections, compare <a href=\"https:\/\/www.pythonpool.com\/binary-search-python\/\">binary search<\/a>, <a href=\"https:\/\/www.pythonpool.com\/python-sorteddict\/\">sorted dictionaries<\/a>, and <a href=\"https:\/\/www.pythonpool.com\/python-heapq\/\">heap-based priority queues<\/a> before choosing a list insertion strategy.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Frequently_Asked_Questions\"><\/span>Frequently Asked Questions<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"What_is_Python_bisect_used_for\"><\/span>What is Python bisect used for?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The bisect module finds where a value belongs in a sorted list and can insert it while preserving sorted order.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"What_is_the_difference_between_bisect_left_and_bisect_right\"><\/span>What is the difference between bisect_left and bisect_right?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>bisect_left returns the first suitable position before equal values, while bisect_right returns the position after existing equal values.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Does_bisect_sort_a_list\"><\/span>Does bisect sort a list?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>No. The input list must already be sorted, and bisect only searches or inserts at the correct position.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Is_bisect_faster_than_searching_a_list\"><\/span>Is bisect faster than searching a list?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The search for an insertion point is logarithmic, but inserting into a Python list still shifts later elements and is linear in the list size.<\/p>\n<p><script type=\"application\/ld+json\">{\"@context\":\"https:\/\/schema.org\",\"@type\":\"FAQPage\",\"mainEntity\":[{\"@type\":\"Question\",\"name\":\"What is Python bisect used for?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"The bisect module finds where a value belongs in a sorted list and can insert it while preserving sorted order.\"}},{\"@type\":\"Question\",\"name\":\"What is the difference between bisect_left and bisect_right?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"bisect_left returns the first suitable position before equal values, while bisect_right returns the position after existing equal values.\"}},{\"@type\":\"Question\",\"name\":\"Does bisect sort a list?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"No. The input list must already be sorted, and bisect only searches or inserts at the correct position.\"}},{\"@type\":\"Question\",\"name\":\"Is bisect faster than searching a list?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"The search for an insertion point is logarithmic, but inserting into a Python list still shifts later elements and is linear in the list size.\"}}]}<\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Use Python&#8217;s bisect module to find insertion points, keep lists sorted, handle duplicates, and choose between bisect and a sorted collection.<\/p>\n","protected":false},"author":3,"featured_media":34208,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[15],"tags":[1209,1210,1190,1195,1211,1200,1189,1198,1212,1213,1203,1208,1194,1202,1188,1196,1207,1201,1206,1191,1205,1197,1192,1199,1204,1193],"class_list":["post-3416","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-root-bisect-python-2","tag-big-o-python-bisect","tag-bisect-in-python","tag-bisect-left-python","tag-bisect-python","tag-bisect-python-2-dimension-array","tag-bisect-python-3","tag-bisect-python-download","tag-bisect-python-install","tag-bisect_left-python","tag-bisect_left-python-3","tag-import-bisect-python-3","tag-python-3-5-bisect","tag-python-bisect-documentation","tag-python-bisect-example","tag-python-bisect-grades","tag-python-bisect-insort","tag-python-bisect-key","tag-python-bisect-left","tag-python-bisect-left-runtime","tag-python-bisect-left-str-lower","tag-python-bisect-with-multiple-variables","tag-python-bisect_left","tag-python-does-bisect-work-with-floats","tag-root-bisect-python","tag-ython-bisect","infinite-scroll-item"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v20.1 (Yoast SEO v28.0) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Python bisect: Search and Insert in Sorted Lists<\/title>\n<meta name=\"description\" content=\"Use Python&#039;s bisect module to find insertion points, keep lists sorted, handle duplicates, and choose between bisect and a sorted collection.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pythonpool.com\/python-bisect\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python bisect: Search and Insert in Sorted Lists\" \/>\n<meta property=\"og:description\" content=\"Use Python&#039;s bisect module to find insertion points, keep lists sorted, handle duplicates, and choose between bisect and a sorted collection.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pythonpool.com\/python-bisect\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Pool\" \/>\n<meta property=\"article:published_time\" content=\"2020-06-28T07:55:33+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-07-13T06:57:01+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-bisect.png\" \/>\n<meta name=\"author\" content=\"Ashwini Mandani\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:title\" content=\"Python Pool\" \/>\n<meta name=\"twitter:description\" content=\"Practical Python tutorials, error fixes, code examples, and project guides.\" \/>\n<meta name=\"twitter:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-bisect.png\" \/>\n<meta name=\"twitter:creator\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:site\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ashwini Mandani\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-bisect\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-bisect\\\/\"},\"author\":{\"name\":\"Ashwini Mandani\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#\\\/schema\\\/person\\\/8a75fb48eb1f3dc952df65a8c28ac056\"},\"headline\":\"Python bisect: Search and Insert in Sorted Lists\",\"datePublished\":\"2020-06-28T07:55:33+00:00\",\"dateModified\":\"2026-07-13T06:57:01+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-bisect\\\/\"},\"wordCount\":1293,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-bisect\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/python-bisect-sorted-list-guide-pythonpool.png\",\"keywords\":[\"&quot;root bisect&quot; python\",\"big o python bisect\",\"bisect in python\",\"bisect left python\",\"bisect python\",\"bisect python 2 dimension array\",\"bisect python 3\",\"bisect python download\",\"bisect python install\",\"bisect_left python\",\"bisect_left python 3\",\"import bisect python 3\",\"python 3.5 bisect\",\"python bisect documentation\",\"python bisect example\",\"python bisect grades\",\"python bisect insort\",\"python bisect key\",\"python bisect left\",\"python bisect left runtime\",\"python bisect left str.lower\",\"python bisect with multiple variables\",\"python bisect_left\",\"python does bisect work with floats\",\"root-bisect python\",\"ython bisect\"],\"articleSection\":[\"Tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.pythonpool.com\\\/python-bisect\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-bisect\\\/\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/python-bisect\\\/\",\"name\":\"Python bisect: Search and Insert in Sorted Lists\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-bisect\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-bisect\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/python-bisect-sorted-list-guide-pythonpool.png\",\"datePublished\":\"2020-06-28T07:55:33+00:00\",\"dateModified\":\"2026-07-13T06:57:01+00:00\",\"description\":\"Use Python's bisect module to find insertion points, keep lists sorted, handle duplicates, and choose between bisect and a sorted collection.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-bisect\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pythonpool.com\\\/python-bisect\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-bisect\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/python-bisect-sorted-list-guide-pythonpool.png\",\"contentUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/python-bisect-sorted-list-guide-pythonpool.png\",\"width\":1350,\"height\":650,\"caption\":\"Python bisect sorted list guide\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-bisect\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pythonpool.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python bisect: Search and Insert in Sorted Lists\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#website\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/\",\"name\":\"Python Pool\",\"description\":\"Practical Python tutorials, error fixes, code examples, and project guides.\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.pythonpool.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#organization\",\"name\":\"Python Pool\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2020\\\/08\\\/aa.png\",\"contentUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2020\\\/08\\\/aa.png\",\"width\":452,\"height\":185,\"caption\":\"Python Pool\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/x.com\\\/pythonpool\",\"https:\\\/\\\/www.youtube.com\\\/c\\\/pythonpool\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#\\\/schema\\\/person\\\/8a75fb48eb1f3dc952df65a8c28ac056\",\"name\":\"Ashwini Mandani\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f765ed1940ac0ae1d30ce4cb2f1452c1e83143b07354d25a42c1cd118980e269?s=96&d=wavatar&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f765ed1940ac0ae1d30ce4cb2f1452c1e83143b07354d25a42c1cd118980e269?s=96&d=wavatar&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f765ed1940ac0ae1d30ce4cb2f1452c1e83143b07354d25a42c1cd118980e269?s=96&d=wavatar&r=g\",\"caption\":\"Ashwini Mandani\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Python bisect: Search and Insert in Sorted Lists","description":"Use Python's bisect module to find insertion points, keep lists sorted, handle duplicates, and choose between bisect and a sorted collection.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pythonpool.com\/python-bisect\/","og_locale":"en_US","og_type":"article","og_title":"Python bisect: Search and Insert in Sorted Lists","og_description":"Use Python's bisect module to find insertion points, keep lists sorted, handle duplicates, and choose between bisect and a sorted collection.","og_url":"https:\/\/www.pythonpool.com\/python-bisect\/","og_site_name":"Python Pool","article_published_time":"2020-06-28T07:55:33+00:00","article_modified_time":"2026-07-13T06:57:01+00:00","og_image":[{"url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-bisect.png","type":"","width":"","height":""}],"author":"Ashwini Mandani","twitter_card":"summary_large_image","twitter_title":"Python Pool","twitter_description":"Practical Python tutorials, error fixes, code examples, and project guides.","twitter_image":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-bisect.png","twitter_creator":"@pythonpool","twitter_site":"@pythonpool","twitter_misc":{"Written by":"Ashwini Mandani","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pythonpool.com\/python-bisect\/#article","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/python-bisect\/"},"author":{"name":"Ashwini Mandani","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/8a75fb48eb1f3dc952df65a8c28ac056"},"headline":"Python bisect: Search and Insert in Sorted Lists","datePublished":"2020-06-28T07:55:33+00:00","dateModified":"2026-07-13T06:57:01+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pythonpool.com\/python-bisect\/"},"wordCount":1293,"commentCount":0,"publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"image":{"@id":"https:\/\/www.pythonpool.com\/python-bisect\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-bisect-sorted-list-guide-pythonpool.png","keywords":["&quot;root bisect&quot; python","big o python bisect","bisect in python","bisect left python","bisect python","bisect python 2 dimension array","bisect python 3","bisect python download","bisect python install","bisect_left python","bisect_left python 3","import bisect python 3","python 3.5 bisect","python bisect documentation","python bisect example","python bisect grades","python bisect insort","python bisect key","python bisect left","python bisect left runtime","python bisect left str.lower","python bisect with multiple variables","python bisect_left","python does bisect work with floats","root-bisect python","ython bisect"],"articleSection":["Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pythonpool.com\/python-bisect\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.pythonpool.com\/python-bisect\/","url":"https:\/\/www.pythonpool.com\/python-bisect\/","name":"Python bisect: Search and Insert in Sorted Lists","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pythonpool.com\/python-bisect\/#primaryimage"},"image":{"@id":"https:\/\/www.pythonpool.com\/python-bisect\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-bisect-sorted-list-guide-pythonpool.png","datePublished":"2020-06-28T07:55:33+00:00","dateModified":"2026-07-13T06:57:01+00:00","description":"Use Python's bisect module to find insertion points, keep lists sorted, handle duplicates, and choose between bisect and a sorted collection.","breadcrumb":{"@id":"https:\/\/www.pythonpool.com\/python-bisect\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pythonpool.com\/python-bisect\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/python-bisect\/#primaryimage","url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-bisect-sorted-list-guide-pythonpool.png","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-bisect-sorted-list-guide-pythonpool.png","width":1350,"height":650,"caption":"Python bisect sorted list guide"},{"@type":"BreadcrumbList","@id":"https:\/\/www.pythonpool.com\/python-bisect\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pythonpool.com\/"},{"@type":"ListItem","position":2,"name":"Python bisect: Search and Insert in Sorted Lists"}]},{"@type":"WebSite","@id":"https:\/\/www.pythonpool.com\/#website","url":"https:\/\/www.pythonpool.com\/","name":"Python Pool","description":"Practical Python tutorials, error fixes, code examples, and project guides.","publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pythonpool.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.pythonpool.com\/#organization","name":"Python Pool","url":"https:\/\/www.pythonpool.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","width":452,"height":185,"caption":"Python Pool"},"image":{"@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/pythonpool","https:\/\/www.youtube.com\/c\/pythonpool"]},{"@type":"Person","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/8a75fb48eb1f3dc952df65a8c28ac056","name":"Ashwini Mandani","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/f765ed1940ac0ae1d30ce4cb2f1452c1e83143b07354d25a42c1cd118980e269?s=96&d=wavatar&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/f765ed1940ac0ae1d30ce4cb2f1452c1e83143b07354d25a42c1cd118980e269?s=96&d=wavatar&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/f765ed1940ac0ae1d30ce4cb2f1452c1e83143b07354d25a42c1cd118980e269?s=96&d=wavatar&r=g","caption":"Ashwini Mandani"}}]}},"_links":{"self":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/3416","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/comments?post=3416"}],"version-history":[{"count":15,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/3416\/revisions"}],"predecessor-version":[{"id":41374,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/3416\/revisions\/41374"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media\/34208"}],"wp:attachment":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media?parent=3416"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/categories?post=3416"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/tags?post=3416"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}