{"id":13369,"date":"2021-06-12T07:47:13","date_gmt":"2021-06-12T02:17:13","guid":{"rendered":"http:\/\/www.pythonpool.com\/?p=13369"},"modified":"2026-07-13T12:34:54","modified_gmt":"2026-07-13T07:04:54","slug":"numpy-searchsorted","status":"publish","type":"post","link":"https:\/\/www.pythonpool.com\/numpy-searchsorted\/","title":{"rendered":"NumPy searchsorted: Find Insertion Indices in Sorted Arrays"},"content":{"rendered":"<p><strong>Quick answer:<\/strong> np.searchsorted() returns insertion indexes for values in a sorted one-dimensional array. side=&#8217;left&#8217; chooses the first valid position before equal values, while side=&#8217;right&#8217; chooses the position after them. The function searches; it does not sort the input, so preserve or verify the sorted invariant first.<\/p>\n<figure class=\"pythonpool-article-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/numpy-searchsorted.png\" alt=\"Python Pool infographic showing NumPy searchsorted sorted array insertion index left right and sorter\" width=\"1536\" height=\"1024\" loading=\"lazy\" decoding=\"async\"><figcaption>searchsorted returns positions that preserve sorted order; side controls duplicate placement and sorter supports an indirectly sorted array.<\/figcaption><\/figure>\n<p><code>np.searchsorted()<\/code> returns the index where a value should be inserted to keep a sorted NumPy array in order. It is a fast way to locate insertion points without scanning manually.<\/p>\n<p>The official <a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.searchsorted.html\">NumPy searchsorted documentation<\/a> explains the full API, and <a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.sort.html\">NumPy sort<\/a> covers sorting arrays before searching.<\/p>\n<p>The input array should already be sorted in ascending order unless you pass a sorter. The function uses binary search internally, so it is useful for lookup tables, bins, thresholds, and inserting new values into ordered data.<\/p>\n<p>Because the function returns positions rather than changing data, it fits well in pipelines. You can calculate insertion points first, inspect them, and then decide whether to insert values, assign labels, or slice existing ranges.<\/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\/numpy-searchsorted\/#Find_One_Insertion_Index\" >Find One Insertion Index<\/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\/numpy-searchsorted\/#Insert_A_Value_After_Searching\" >Insert A Value After Searching<\/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\/numpy-searchsorted\/#Choose_Left_Or_Right_Side\" >Choose Left Or Right Side<\/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\/numpy-searchsorted\/#Search_Several_Values_At_Once\" >Search Several Values At Once<\/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\/numpy-searchsorted\/#Use_Searchsorted_For_Buckets\" >Use Searchsorted For Buckets<\/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\/numpy-searchsorted\/#Search_With_A_Sorter\" >Search With A Sorter<\/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\/numpy-searchsorted\/#Common_Mistakes\" >Common Mistakes<\/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\/numpy-searchsorted\/#Find_One_Insertion_Position\" >Find One Insertion Position<\/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\/numpy-searchsorted\/#Choose_left_Or_right_For_Duplicates\" >Choose left Or right For Duplicates<\/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\/numpy-searchsorted\/#Search_Many_Values\" >Search Many Values<\/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\/numpy-searchsorted\/#Use_sorter_For_Indirect_Order\" >Use sorter For Indirect Order<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.pythonpool.com\/numpy-searchsorted\/#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-13\" href=\"https:\/\/www.pythonpool.com\/numpy-searchsorted\/#What_does_NumPy_searchsorted_return\" >What does NumPy searchsorted return?<\/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\/numpy-searchsorted\/#What_is_the_difference_between_sideleft_and_sideright\" >What is the difference between side=&#8217;left&#8217; and side=&#8217;right&#8217;?<\/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\/numpy-searchsorted\/#Does_searchsorted_sort_the_input_array\" >Does searchsorted sort the input array?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/www.pythonpool.com\/numpy-searchsorted\/#Can_searchsorted_handle_many_values\" >Can searchsorted handle many values?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"Find_One_Insertion_Index\"><\/span>Find One Insertion Index<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Pass a sorted array and one value to get the insertion 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 numpy as np\n\nvalues = np.array([2, 4, 8, 9])\nindex = np.searchsorted(values, 6)\n\nprint(index)\n<\/code><\/pre>\n<\/div>\n<p>The result is <code>2<\/code> because <code>6<\/code> belongs between <code>4<\/code> and <code>8<\/code>. Inserting at that index keeps the array sorted.<\/p>\n<p>The returned position is an index, not a modified array. Use it with another operation when you actually need to insert data.<\/p>\n<p>Indexes can be equal to the length of the array. That simply means the value belongs after the last item, which is a valid insertion point.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Insert_A_Value_After_Searching\"><\/span>Insert A Value After Searching<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><code>searchsorted()<\/code> pairs naturally with <code>np.insert()<\/code> when you want the updated array.<\/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 numpy as np\n\nvalues = np.array([2, 4, 8, 9])\nnew_value = 6\n\nindex = np.searchsorted(values, new_value)\nupdated = np.insert(values, index, new_value)\n\nprint(updated)\n<\/code><\/pre>\n<\/div>\n<p>This inserts <code>6<\/code> at the correct position. For occasional inserts, this pattern is clear and simple.<\/p>\n<p>For many inserts into a large array, collect the new values and sort or merge in batches. Repeated array insertion can be expensive because NumPy arrays have fixed-size storage.<\/p>\n<p>If you need a data structure that receives many small inserts, a list or specialized sorted container may be more appropriate until the final array is needed.<\/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\/searchsorted-sorted-b172.png\" alt=\"Python Pool infographic showing a sorted NumPy array, target values, and insertion positions\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>Sorted values: A sorted NumPy array, target values, and insertion positions.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Choose_Left_Or_Right_Side\"><\/span>Choose Left Or Right Side<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>When the value already exists, the <code>side<\/code> argument decides whether the insertion point goes before or after the existing matching values.<\/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 numpy as np\n\nvalues = np.array([1, 3, 3, 3, 7])\n\nleft = np.searchsorted(values, 3, side=\"left\")\nright = np.searchsorted(values, 3, side=\"right\")\n\nprint(left)\nprint(right)\n<\/code><\/pre>\n<\/div>\n<p><code>side=\"left\"<\/code> returns the first matching position. <code>side=\"right\"<\/code> returns the position after the last matching value.<\/p>\n<p>This is useful for finding ranges in sorted data. The left and right positions can define the slice that contains all matching values.<\/p>\n<p>For example, <code>values[left:right]<\/code> would contain every existing match. If <code>left<\/code> and <code>right<\/code> are equal, the value is not present even though the insertion point is still useful.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Search_Several_Values_At_Once\"><\/span>Search Several Values At Once<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The second argument can be an array of values. NumPy returns one insertion index for each value.<\/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 numpy as np\n\nbreakpoints = np.array([10, 20, 30, 40])\nqueries = np.array([5, 10, 25, 50])\n\nindexes = np.searchsorted(breakpoints, queries)\n\nprint(indexes)\n<\/code><\/pre>\n<\/div>\n<p>Each query is compared with the sorted breakpoints. The result can be used for binning, labeling, or selecting threshold ranges.<\/p>\n<p>When using indexes for bins, check boundary cases carefully. Values below the first item and above the last item return edge positions.<\/p>\n<p>These edge positions are often exactly what you want for labels, but they should be tested. A value below the first breakpoint returns <code>0<\/code>, and a value above all breakpoints returns the number of breakpoints.<\/p>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/searchsorted-side-b172.png\" alt=\"Python Pool infographic comparing side left, side right, duplicate values, and insertion indices\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>Left or right: Side left, side right, duplicate values, and insertion indices.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Use_Searchsorted_For_Buckets\"><\/span>Use Searchsorted For Buckets<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Threshold buckets are a common practical use. The insertion index tells you how many thresholds the value has passed.<\/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 numpy as np\n\nthresholds = np.array([60, 70, 80, 90])\nscores = np.array([55, 72, 88, 94])\nlabels = np.array([\"F\", \"D\", \"C\", \"B\", \"A\"])\n\nbucket_indexes = np.searchsorted(thresholds, scores, side=\"right\")\n\nprint(labels[bucket_indexes])\n<\/code><\/pre>\n<\/div>\n<p>Using <code>side=\"right\"<\/code> means a score equal to a threshold moves into the higher bucket. Choose the side based on the rule your grading or binning system needs.<\/p>\n<p>This approach keeps the bucket logic compact and avoids a long chain of conditional branches.<\/p>\n<p>The same pattern works for tax brackets, score bands, latency buckets, or any sorted threshold list where each input should map to one label.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Search_With_A_Sorter\"><\/span>Search With A Sorter<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>If the original array is not sorted, compute a sorter with <code>np.argsort()<\/code> and pass it to <code>searchsorted()<\/code>.<\/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 numpy as np\n\nvalues = np.array([40, 10, 30, 20])\nsorter = np.argsort(values)\n\nindex = np.searchsorted(values, 25, sorter=sorter)\nsorted_values = values[sorter]\n\nprint(sorted_values)\nprint(index)\n<\/code><\/pre>\n<\/div>\n<p>The sorter describes the order that would sort the original array. Search results are positions in that sorted order.<\/p>\n<p>If you will search repeatedly, keeping a sorted copy is often easier to reason about than repeatedly passing a sorter.<\/p>\n<p>The sorter form is helpful when you must keep the original order for later reporting. For everyday lookups, a sorted working array is simpler and less error-prone.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Common_Mistakes\"><\/span>Common Mistakes<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Do not call <code>searchsorted()<\/code> on unsorted data without a sorter. The result depends on sorted order and can be wrong when the array is not ordered.<\/p>\n<p>Do not confuse the returned index with the value at that index. The function tells you where a value belongs, not what value is already there.<\/p>\n<p>Do not forget duplicate handling. Use <code>side=\"left\"<\/code> for the first acceptable position and <code>side=\"right\"<\/code> for the position after existing matches.<\/p>\n<p>The practical workflow is simple: sort the reference array, call <code>np.searchsorted()<\/code> to get insertion indexes, then use those indexes for insertion, binning, slicing, or threshold labels.<\/p>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/searchsorted-sorter-b172.png\" alt=\"Python Pool infographic mapping an unsorted array through sorter indices to searchsorted positions\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>Sorter index: An unsorted array through sorter indices to searchsorted positions.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Find_One_Insertion_Position\"><\/span>Find One Insertion Position<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>For a sorted array, searchsorted returns the location where a value can be inserted without breaking order. The index may be zero or the array length when the value belongs before every item or after every item.<\/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 numpy as np\n\nvalues = np.array([10, 20, 30, 40])\nfor target in [5, 25, 50]:\n    print(target, np.searchsorted(values, target))<\/code><\/pre>\n<\/div>\n<h2><span class=\"ez-toc-section\" id=\"Choose_left_Or_right_For_Duplicates\"><\/span>Choose left Or right For Duplicates<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>With duplicates, left returns the first suitable index and right returns the last suitable insertion position. Use the side that matches whether new equal records should precede or follow existing records.<\/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 numpy as np\n\nvalues = np.array([10, 20, 20, 20, 40])\nprint(np.searchsorted(values, 20, side=\"left\"))\nprint(np.searchsorted(values, 20, side=\"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\/searchsorted-check-b172.png\" alt=\"Python Pool infographic testing bounds, duplicates, axis, dtype, and sortedness\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>Search checks: Bounds, duplicates, axis, dtype, and sortedness.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Search_Many_Values\"><\/span>Search Many Values<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>v can be a scalar or an array-like collection, so one call can produce insertion positions for many targets. The result shape follows v. Validate that the values use a comparable dtype and that the sorted order means what the application expects.<\/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 numpy as np\n\nvalues = np.array([0, 10, 20, 30])\ntargets = np.array([3, 12, 29])\nprint(np.searchsorted(values, targets, side=\"left\"))<\/code><\/pre>\n<\/div>\n<h2><span class=\"ez-toc-section\" id=\"Use_sorter_For_Indirect_Order\"><\/span>Use sorter For Indirect Order<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>When the source array is not physically sorted, pass sorter as the indexes that would sort it. searchsorted then searches the logical ascending order while returning positions within that order. Keep the sorter paired with the same array and update both when the data changes.<\/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 numpy as np\n\nvalues = np.array([30, 10, 20])\nsorter = np.argsort(values)\nprint(sorter)\nprint(np.searchsorted(values, [15, 25], sorter=sorter))<\/code><\/pre>\n<\/div>\n<p>NumPy&#8217;s official <a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.searchsorted.html\">searchsorted reference<\/a> defines side, sorter, vectorized values, and the sorted-array invariant. Use it for insertion positions, not as a hidden sorting step.<\/p>\n<p>For related ordered-array operations, compare <a href=\"https:\/\/www.pythonpool.com\/python-bisect\/\">Python bisect<\/a>, <a href=\"https:\/\/www.pythonpool.com\/numpy-arange\/\">NumPy arange()<\/a>, and <a href=\"https:\/\/www.pythonpool.com\/numpy-reshape\/\">NumPy reshape()<\/a> before choosing an index search for duplicates.<\/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_does_NumPy_searchsorted_return\"><\/span>What does NumPy searchsorted return?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>It returns the index or indexes where values can be inserted into a sorted one-dimensional array while preserving order.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"What_is_the_difference_between_sideleft_and_sideright\"><\/span>What is the difference between side=&#8217;left&#8217; and side=&#8217;right&#8217;?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>left returns the first suitable position before equal values, while right returns the position after equal values.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Does_searchsorted_sort_the_input_array\"><\/span>Does searchsorted sort the input array?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>No. The array must already be sorted unless you pass sorter with indexes that represent its ascending order.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Can_searchsorted_handle_many_values\"><\/span>Can searchsorted handle many values?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Yes. Pass an array-like v and it returns a NumPy array of insertion positions using vectorized comparison logic.<\/p>\n<p><script type=\"application\/ld+json\">{\"@context\":\"https:\/\/schema.org\",\"@type\":\"FAQPage\",\"mainEntity\":[{\"@type\":\"Question\",\"name\":\"What does NumPy searchsorted return?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"It returns the index or indexes where values can be inserted into a sorted one-dimensional array while preserving order.\"}},{\"@type\":\"Question\",\"name\":\"What is the difference between side='left' and side='right'?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"left returns the first suitable position before equal values, while right returns the position after equal values.\"}},{\"@type\":\"Question\",\"name\":\"Does searchsorted sort the input array?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"No. The array must already be sorted unless you pass sorter with indexes that represent its ascending order.\"}},{\"@type\":\"Question\",\"name\":\"Can searchsorted handle many values?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Yes. Pass an array-like v and it returns a NumPy array of insertion positions using vectorized comparison logic.\"}}]}<\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Use NumPy searchsorted with sorted arrays, left and right sides, vectorized values, sorter indexes, and duplicate handling.<\/p>\n","protected":false},"author":20,"featured_media":33749,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1495],"tags":[4204,4207,4202,4209,4203],"class_list":["post-13369","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-numpy","tag-how-does-numpy-searchsorted-work","tag-numpy-searchsorted-2d-array","tag-numpy-searchsorted-function-in-python","tag-numpy-searchsorted-parallel","tag-what-is-numpy-searchsorted","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>NumPy searchsorted: Find Insertion Indices in Sorted Arrays<\/title>\n<meta name=\"description\" content=\"Use NumPy searchsorted with sorted arrays, left and right sides, vectorized values, sorter indexes, and duplicate handling.\" \/>\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\/numpy-searchsorted\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"NumPy searchsorted: Find Insertion Indices in Sorted Arrays\" \/>\n<meta property=\"og:description\" content=\"Use NumPy searchsorted with sorted arrays, left and right sides, vectorized values, sorter indexes, and duplicate handling.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pythonpool.com\/numpy-searchsorted\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Pool\" \/>\n<meta property=\"article:published_time\" content=\"2021-06-12T02:17:13+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-07-13T07:04:54+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/numpy-searchsorted.png\" \/>\n<meta name=\"author\" content=\"Dhruvi Vikma\" \/>\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\/numpy-searchsorted.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=\"Dhruvi Vikma\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-searchsorted\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-searchsorted\\\/\"},\"author\":{\"name\":\"Dhruvi Vikma\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#\\\/schema\\\/person\\\/bed8fc40c7b71baf7d76b1cfefd79f23\"},\"headline\":\"NumPy searchsorted: Find Insertion Indices in Sorted Arrays\",\"datePublished\":\"2021-06-12T02:17:13+00:00\",\"dateModified\":\"2026-07-13T07:04:54+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-searchsorted\\\/\"},\"wordCount\":1169,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-searchsorted\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/numpy-searchsorted-guide-pythonpool.png\",\"keywords\":[\"how does numpy searchsorted work\",\"numpy searchsorted 2d array\",\"Numpy Searchsorted Function in python\",\"numpy searchsorted parallel\",\"what is numpy searchsorted\"],\"articleSection\":[\"Numpy\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.pythonpool.com\\\/numpy-searchsorted\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-searchsorted\\\/\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-searchsorted\\\/\",\"name\":\"NumPy searchsorted: Find Insertion Indices in Sorted Arrays\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-searchsorted\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-searchsorted\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/numpy-searchsorted-guide-pythonpool.png\",\"datePublished\":\"2021-06-12T02:17:13+00:00\",\"dateModified\":\"2026-07-13T07:04:54+00:00\",\"description\":\"Use NumPy searchsorted with sorted arrays, left and right sides, vectorized values, sorter indexes, and duplicate handling.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-searchsorted\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pythonpool.com\\\/numpy-searchsorted\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-searchsorted\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/numpy-searchsorted-guide-pythonpool.png\",\"contentUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/numpy-searchsorted-guide-pythonpool.png\",\"width\":1350,\"height\":650,\"caption\":\"NumPy searchsorted guide\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/numpy-searchsorted\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pythonpool.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"NumPy searchsorted: Find Insertion Indices in Sorted Arrays\"}]},{\"@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\\\/bed8fc40c7b71baf7d76b1cfefd79f23\",\"name\":\"Dhruvi Vikma\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/520d8a37e8b6ce4e6df57e997dc0d571225f48098bbb02838c3884bb603e886f?s=96&d=wavatar&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/520d8a37e8b6ce4e6df57e997dc0d571225f48098bbb02838c3884bb603e886f?s=96&d=wavatar&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/520d8a37e8b6ce4e6df57e997dc0d571225f48098bbb02838c3884bb603e886f?s=96&d=wavatar&r=g\",\"caption\":\"Dhruvi Vikma\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"NumPy searchsorted: Find Insertion Indices in Sorted Arrays","description":"Use NumPy searchsorted with sorted arrays, left and right sides, vectorized values, sorter indexes, and duplicate handling.","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\/numpy-searchsorted\/","og_locale":"en_US","og_type":"article","og_title":"NumPy searchsorted: Find Insertion Indices in Sorted Arrays","og_description":"Use NumPy searchsorted with sorted arrays, left and right sides, vectorized values, sorter indexes, and duplicate handling.","og_url":"https:\/\/www.pythonpool.com\/numpy-searchsorted\/","og_site_name":"Python Pool","article_published_time":"2021-06-12T02:17:13+00:00","article_modified_time":"2026-07-13T07:04:54+00:00","og_image":[{"url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/numpy-searchsorted.png","type":"","width":"","height":""}],"author":"Dhruvi Vikma","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\/numpy-searchsorted.png","twitter_creator":"@pythonpool","twitter_site":"@pythonpool","twitter_misc":{"Written by":"Dhruvi Vikma","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pythonpool.com\/numpy-searchsorted\/#article","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/numpy-searchsorted\/"},"author":{"name":"Dhruvi Vikma","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/bed8fc40c7b71baf7d76b1cfefd79f23"},"headline":"NumPy searchsorted: Find Insertion Indices in Sorted Arrays","datePublished":"2021-06-12T02:17:13+00:00","dateModified":"2026-07-13T07:04:54+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pythonpool.com\/numpy-searchsorted\/"},"wordCount":1169,"commentCount":0,"publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"image":{"@id":"https:\/\/www.pythonpool.com\/numpy-searchsorted\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/numpy-searchsorted-guide-pythonpool.png","keywords":["how does numpy searchsorted work","numpy searchsorted 2d array","Numpy Searchsorted Function in python","numpy searchsorted parallel","what is numpy searchsorted"],"articleSection":["Numpy"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pythonpool.com\/numpy-searchsorted\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.pythonpool.com\/numpy-searchsorted\/","url":"https:\/\/www.pythonpool.com\/numpy-searchsorted\/","name":"NumPy searchsorted: Find Insertion Indices in Sorted Arrays","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pythonpool.com\/numpy-searchsorted\/#primaryimage"},"image":{"@id":"https:\/\/www.pythonpool.com\/numpy-searchsorted\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/numpy-searchsorted-guide-pythonpool.png","datePublished":"2021-06-12T02:17:13+00:00","dateModified":"2026-07-13T07:04:54+00:00","description":"Use NumPy searchsorted with sorted arrays, left and right sides, vectorized values, sorter indexes, and duplicate handling.","breadcrumb":{"@id":"https:\/\/www.pythonpool.com\/numpy-searchsorted\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pythonpool.com\/numpy-searchsorted\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/numpy-searchsorted\/#primaryimage","url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/numpy-searchsorted-guide-pythonpool.png","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/numpy-searchsorted-guide-pythonpool.png","width":1350,"height":650,"caption":"NumPy searchsorted guide"},{"@type":"BreadcrumbList","@id":"https:\/\/www.pythonpool.com\/numpy-searchsorted\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pythonpool.com\/"},{"@type":"ListItem","position":2,"name":"NumPy searchsorted: Find Insertion Indices in Sorted Arrays"}]},{"@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\/bed8fc40c7b71baf7d76b1cfefd79f23","name":"Dhruvi Vikma","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/520d8a37e8b6ce4e6df57e997dc0d571225f48098bbb02838c3884bb603e886f?s=96&d=wavatar&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/520d8a37e8b6ce4e6df57e997dc0d571225f48098bbb02838c3884bb603e886f?s=96&d=wavatar&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/520d8a37e8b6ce4e6df57e997dc0d571225f48098bbb02838c3884bb603e886f?s=96&d=wavatar&r=g","caption":"Dhruvi Vikma"}}]}},"_links":{"self":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/13369","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\/20"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/comments?post=13369"}],"version-history":[{"count":23,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/13369\/revisions"}],"predecessor-version":[{"id":41684,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/13369\/revisions\/41684"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media\/33749"}],"wp:attachment":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media?parent=13369"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/categories?post=13369"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/tags?post=13369"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}