{"id":3416,"date":"2020-06-28T13:25:33","date_gmt":"2020-06-28T07:55:33","guid":{"rendered":"http:\/\/www.pythonpool.com\/?p=3416"},"modified":"2022-02-21T10:39:44","modified_gmt":"2022-02-21T05:09:44","slug":"python-bisect","status":"publish","type":"post","link":"https:\/\/www.pythonpool.com\/python-bisect\/","title":{"rendered":"Python Bisect | Working With Python Bisect Module"},"content":{"rendered":"\n<p><strong>Python bisect module<\/strong> comes handy when we need to insert new data while maintaining the data in sorted order. Without this module, it would have been a real pain for our systems&#8217; processor. Our data remains sorted when a new insertion is made. The only thing we have to keep in mind is that the data is already in sorted order.&nbsp;&nbsp;<\/p>\n\n\n\n<p>Let us learn about some the use cases of <strong>Python bisect<\/strong> and why it is so useful in real life.&nbsp;<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_74 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\/#Importing_Python_bisect_module_%E2%80%93\" >Importing Python bisect module &#8211;&nbsp;<\/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\/#Various_functions_in_python_bisect_module\" >Various functions in python bisect module&nbsp;<\/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\/#Bisect_a_x_lo_hi\" >Bisect (a, x[, lo[, hi]])<\/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\/#bisect_lefta_x_lo_hi\" >bisect_left(a, x[, lo[, hi]])<\/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\/#bisect_righta_x_lo_hi\" >bisect_right(a, x[, lo[, hi]])<\/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\/#insorta_x_lo_hi\" >insort(a, x[, lo[, hi]])<\/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\/#_insort_left\" >&nbsp;insort_left()<\/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\/#Insort_right\" >Insort_right()-<\/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\/#Program_to_find_the_index_without_python_bisect_module\" >Program to find the index without python bisect module-&nbsp;<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.pythonpool.com\/python-bisect\/#Lets_make_a_program_working_the_same_as_python_bisectbisect\" >Let&#8217;s make a program working the same as python bisect.bisect().&nbsp;<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.pythonpool.com\/python-bisect\/#Now_lets_program_for_insort\" >Now let\u2019s program for insort()-&nbsp;<\/a><\/li><\/ul><\/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\/python-bisect\/#Some_More_Examples\" >Some More Examples-&nbsp;<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/www.pythonpool.com\/python-bisect\/#Limitations_of_Python_Bisect\" >Limitations of Python Bisect()-&nbsp;<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/www.pythonpool.com\/python-bisect\/#Must_Read\" >Must Read:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/www.pythonpool.com\/python-bisect\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"h-importing-python-bisect-module\"><span class=\"ez-toc-section\" id=\"Importing_Python_bisect_module_%E2%80%93\"><\/span>Importing Python bisect module &#8211;&nbsp;<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Python bisect module comes preinstalled with python, and we need to import it before using it. The only prerequisite for using this module is that the data structure should already be sorted. Otherwise it would not give correct answers.&nbsp; &nbsp;<\/p>\n\n\n\n<p>To import this module, use &#8211;<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport bisect \n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-various-functions-in-python-bisect-module\"><span class=\"ez-toc-section\" id=\"Various_functions_in_python_bisect_module\"><\/span>Various functions in python bisect module&nbsp;<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>There is a total of 6 functions available in this module, each having its applications. Let&#8217;s learn each of them.&nbsp;&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-bisect-a-x-lo-hi\"><span class=\"ez-toc-section\" id=\"Bisect_a_x_lo_hi\"><\/span>Bisect (a, x[, lo[, hi]])<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p> This function, when used, returns the index at which you should insert the given element in the given list, keeping the list sorted.&nbsp;&nbsp;<\/p>\n\n\n\n<p><strong>The first argument is the list where you want to make the insertion, x is the element we want to insert. Lo and hi are the starting and ending index of the list you want to consider. If we do not pass the values in the last two parameters, the whole list would be considered. These functions can be used on both the numeric list and the list containing the names.&nbsp;&nbsp;<\/strong><\/p>\n\n\n\n<p>Suppose we have a list containing the marks in the sorted order from highest to lowest. And we want to enter new marks in the same list containing the list sorted.&nbsp;<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nmarks=&#x5B;33,36,40,43,44,48,50] \nnew_marks=42 \nprint(bisect.bisect(marks,new_marks)) \n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">Output-&nbsp;\n3&nbsp;<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1584\" height=\"436\" src=\"https:\/\/i0.wp.com\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-60.png?fit=1024%2C282&amp;ssl=1\" alt=\"python bisect\" class=\"wp-image-3417\" srcset=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-60.png 1584w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-60-300x83.png 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-60-1024x282.png 1024w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-60-768x211.png 768w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-60-1536x423.png 1536w\" sizes=\"(max-width: 1584px) 100vw, 1584px\" \/><\/figure>\n\n\n\n<p>Note- Remember that the index of the list starts from index 0.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-bisect_left-a-x-lo-hi\"><span class=\"ez-toc-section\" id=\"bisect_lefta_x_lo_hi\"><\/span>bisect_left(a, x[, lo[, hi]]) <span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p> Suppose that the element you want to insert is already present in the list, bisect_left() returns the index before that element. Let us look at an example to understand it better.&nbsp;&nbsp;<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nmarks=&#x5B;33,36,40,42,43,44,48,50]\nnew_marks=42 \nbisect.bisect_left(marks,new_marks) \n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">Output-&nbsp;\n3&nbsp;&nbsp;<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1584\" height=\"436\" src=\"https:\/\/i2.wp.com\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-61.png?fit=1024%2C282&amp;ssl=1\" alt=\"python bisect\" class=\"wp-image-3418\" srcset=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-61.png 1584w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-61-300x83.png 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-61-1024x282.png 1024w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-61-768x211.png 768w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/image-61-1536x423.png 1536w\" sizes=\"(max-width: 1584px) 100vw, 1584px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-bisect_right-a-x-lo-hi\"><span class=\"ez-toc-section\" id=\"bisect_righta_x_lo_hi\"><\/span>bisect_right(a, x[, lo[, hi]]) <span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Unlike the bisect_left() function, if the element is already present, this function returns the index after that element.&nbsp;<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nmarks=&#x5B;33,36,40,42,43,44,48,50] \nnew_marks=42 \nbisect.bisect_right(marks,new_marks) \n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">Output-&nbsp;\n4&nbsp;<\/pre>\n\n\n\n<p>&nbsp;Note that the bisect() function works same as bisect_right() and is just a shorthand for bisect_right().&nbsp;<\/p>\n\n\n\n<p>The time <a href=\"https:\/\/en.wikipedia.org\/wiki\/Complexity\" target=\"_blank\" aria-label=\"undefined (opens in a new tab)\" rel=\"noreferrer noopener\">complexity<\/a> of these functions is<strong> O(log(n))<\/strong>, as it is based on the concept of binary search.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-insort-a-x-lo-hi\"><span class=\"ez-toc-section\" id=\"insorta_x_lo_hi\"><\/span>insort(a, x[, lo[, hi]]) <span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>This function is used to insert the element in the list without disturbing the order of the list. Other things like parameters are precisely the same as bisect().&nbsp;<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumbers=&#x5B;1,2,5,7,11,13,19] \nnew_element=17 \nbisect.insort(numbers,new_element) \nprint(numbers) \n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">Output-\n[1, 2, 5, 7, 11, 13, 17, 19]&nbsp;<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-insort_left\"><span class=\"ez-toc-section\" id=\"_insort_left\"><\/span>&nbsp;insort_left()<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>It is similar to bisect_left(). The insertion takes place before the already present element.&nbsp;<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumbers=&#x5B;1,2,5,7,11,13,17,19] \nnew_element=17 \nbisect.insort(numbers,new_element) \nprint(numbers) \n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">Output-\n[1, 2, 5, 7, 11, 13, 17, 19]&nbsp;<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-insort_right\"><span class=\"ez-toc-section\" id=\"Insort_right\"><\/span>Insort_right()- <span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>It works same as insort().&nbsp;<\/p>\n\n\n\n<p>Note- The time complexity of insort() is<strong> O(n)<\/strong>.&nbsp;<\/p>\n\n\n\n<p>Now that we have learned about these functions let us try to search for the index without using the python bisect module.&nbsp;&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-program-to-find-the-index-without-python-bisect-module\"><span class=\"ez-toc-section\" id=\"Program_to_find_the_index_without_python_bisect_module\"><\/span>Program to find the index without python bisect module-&nbsp;<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-let-s-make-a-program-working-the-same-as-python-bisect-bisect\"><span class=\"ez-toc-section\" id=\"Lets_make_a_program_working_the_same_as_python_bisectbisect\"><\/span>Let&#8217;s make a program working the same as python bisect.bisect().&nbsp;<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumbers=&#x5B;10,12,14,17,19.2,22,23] \nx=17 \nfor i in range(len(numbers)): \n    if numbers&#x5B;i] &lt; x: \n       pass \n   #For bisect.left() remove the below elif statement \n    elif numbers&#x5B;i] == x: \n       pass \n   else: \n       print(i) \n       break \n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">Output\n4<\/pre>\n\n\n\n<p>This much of code we have to <a href=\"http:\/\/www.pythonpool.com\/guest-post\/\" target=\"_blank\" rel=\"noreferrer noopener\">write<\/a> when we want to search for index without using the bisect(). Also, this program takes much more time to implement than bisect() when we have lots and lots of data.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-now-let-s-program-for-insort\"><span class=\"ez-toc-section\" id=\"Now_lets_program_for_insort\"><\/span>Now let\u2019s program for insort()-&nbsp;<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumbers=&#x5B;10,12,14,17,19.2,22,23] \nx=17 \nfor i in range(len(numbers)): \n    if numbers&#x5B;i] &lt; x: \n       pass \n   #For insort.left() remove the below elif statement \n    elif numbers&#x5B;i] == x: \n       pass \n   else: \n       numbers&#x5B;i]=x \n       break \n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">Output-\n[10,12,14,17,17,19.2,22,23]&nbsp;<\/pre>\n\n\n\n<p>&nbsp;&nbsp;Now you can think about how easy the python bisect module makes it for us.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-some-more-examples\"><span class=\"ez-toc-section\" id=\"Some_More_Examples\"><\/span>Some More Examples-&nbsp;<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Let us see how can bisect() help us in strings-&nbsp;<\/li><\/ul>\n\n\n\n<p>Suppose you have a list of student names in sorted order. Now, a new student named \u2018Manish\u2019 takes admission. Now let us see how we can add his name keeping the list sorted.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnames=&#x5B;'Ashwini','Bulbul','Chetan','Naman','Zubair'] \nnew='Manish' \nbisect.insort(names,new) \nprint(names) \n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">Output-\n['Ashwini', 'Bulbul', 'Chetan', 'Manish', 'Naman', 'Zubair']&nbsp;<\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>Let, suppose we want to add a list of marks in our marks list.&nbsp;&nbsp;<\/li><\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nmarks=&#x5B;21,30,31,33,36,40,43,44,48,50]  \nnewlist=&#x5B;32,48,29] \nfor i in newlist: \n    bisect.insort(marks,i) \nprint(marks) \n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">Output-\n[21, 29, 30, 31, 32, 33, 36, 40, 43, 44, 48, 48, 50]&nbsp;<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-limitations-of-python-bisect\"><span class=\"ez-toc-section\" id=\"Limitations_of_Python_Bisect\"><\/span>Limitations of Python Bisect()-&nbsp;<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>We need elements in sorted order. To solve this problem, we can use the python sort() function.&nbsp;<\/li><\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nmarks=&#x5B;21,30,43,12,29,50,48]  \nmarks.sort() \nnewlist=&#x5B;32,48,29] \nfor i in newlist: \n    bisect.insort(marks,i) \nprint(marks) \n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">Output-\n[12,21,29,29,30,32,43,48,48,50]<\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>If the list is in descending order, we first need to convert it to ascending order.&nbsp;<\/li><\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nmarks=&#x5B;50,49,48,47,46] \nmarks.sort() \nnewlist=&#x5B;32,48,29] \nfor i in newlist: \n    bisect.insort(marks,i) \nprint(marks&#x5B;::-1]) \n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">Output-\n[50, 49, 48, 48, 47, 46, 32, 29]&nbsp;<\/pre>\n\n\n\n<h2 class=\"has-vivid-red-color has-text-color wp-block-heading\" id=\"h-must-read\"><span class=\"ez-toc-section\" id=\"Must_Read\"><\/span>Must Read:<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"http:\/\/www.pythonpool.com\/python-lowercase\/\">How to Convert String to Lowercase in<\/a><\/li><li><a href=\"http:\/\/www.pythonpool.com\/square-root-in-python\/\">How to Calculate Square Root<\/a><\/li><li><a href=\"http:\/\/www.pythonpool.com\/python-user-input\/\">User Input | Input () Function | Keyboard Input<\/a><\/li><li><a href=\"http:\/\/www.pythonpool.com\/python-book\/\">Best Book to Learn Python<\/a><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>As we have seen there are multiple use cases of the python bisect module. If it was not there we will have to write lots and lots of code and also it would have caused a lot of strain on our processors.<\/p>\n\n\n\n<p>Try to run the programs on your side and let me know if you have any queries.<\/p>\n\n\n\n<p><strong><em>Happy Coding!<\/em><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python bisect module comes handy when we need to insert new data while maintaining the data in sorted order. Without this module, it would have &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Python Bisect | Working With Python Bisect Module\" class=\"read-more button\" href=\"https:\/\/www.pythonpool.com\/python-bisect\/#more-3416\" aria-label=\"More on Python Bisect | Working With Python Bisect Module\">Read more<\/a><\/p>\n","protected":false},"author":3,"featured_media":3421,"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 v25.0) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python Bisect | Working With Python Bisect Module - Python Pool<\/title>\n<meta name=\"description\" content=\"When we want to insert data keeping the list sorted, we use the python bisect module. Python bisect has six functions and each of them has its own uses.\" \/>\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 | Working With Python Bisect Module\" \/>\n<meta property=\"og:description\" content=\"Python bisect module comes handy when we need to insert new data while maintaining the data in sorted order. Without this module, it would have been a\" \/>\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=\"2022-02-21T05:09:44+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/Capture-10.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1001\" \/>\n\t<meta property=\"og:image:height\" content=\"603\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Ashwini Mandani\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\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 | Working With Python Bisect Module\",\"datePublished\":\"2020-06-28T07:55:33+00:00\",\"dateModified\":\"2022-02-21T05:09:44+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-bisect\/\"},\"wordCount\":801,\"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\/2020\/06\/Capture-10.jpg\",\"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 | Working With Python Bisect Module - Python Pool\",\"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\/2020\/06\/Capture-10.jpg\",\"datePublished\":\"2020-06-28T07:55:33+00:00\",\"dateModified\":\"2022-02-21T05:09:44+00:00\",\"description\":\"When we want to insert data keeping the list sorted, we use the python bisect module. Python bisect has six functions and each of them has its own uses.\",\"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\/2020\/06\/Capture-10.jpg\",\"contentUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/Capture-10.jpg\",\"width\":1001,\"height\":603,\"caption\":\"python bisect\"},{\"@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 | Working With Python Bisect Module\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.pythonpool.com\/#website\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"name\":\"Python Pool\",\"description\":\"Your One-Stop Python Learning Destination\",\"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\":\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"contentUrl\":\"http:\/\/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:\/\/www.pythonpool.com\/#\/schema\/person\/image\/\",\"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 | Working With Python Bisect Module - Python Pool","description":"When we want to insert data keeping the list sorted, we use the python bisect module. Python bisect has six functions and each of them has its own uses.","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 | Working With Python Bisect Module","og_description":"Python bisect module comes handy when we need to insert new data while maintaining the data in sorted order. Without this module, it would have been a","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":"2022-02-21T05:09:44+00:00","og_image":[{"width":1001,"height":603,"url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/Capture-10.jpg","type":"image\/jpeg"}],"author":"Ashwini Mandani","twitter_card":"summary_large_image","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 | Working With Python Bisect Module","datePublished":"2020-06-28T07:55:33+00:00","dateModified":"2022-02-21T05:09:44+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pythonpool.com\/python-bisect\/"},"wordCount":801,"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\/2020\/06\/Capture-10.jpg","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 | Working With Python Bisect Module - Python Pool","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\/2020\/06\/Capture-10.jpg","datePublished":"2020-06-28T07:55:33+00:00","dateModified":"2022-02-21T05:09:44+00:00","description":"When we want to insert data keeping the list sorted, we use the python bisect module. Python bisect has six functions and each of them has its own uses.","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\/2020\/06\/Capture-10.jpg","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/06\/Capture-10.jpg","width":1001,"height":603,"caption":"python bisect"},{"@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 | Working With Python Bisect Module"}]},{"@type":"WebSite","@id":"https:\/\/www.pythonpool.com\/#website","url":"https:\/\/www.pythonpool.com\/","name":"Python Pool","description":"Your One-Stop Python Learning Destination","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":"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","contentUrl":"http:\/\/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:\/\/www.pythonpool.com\/#\/schema\/person\/image\/","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":5,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/3416\/revisions"}],"predecessor-version":[{"id":13659,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/3416\/revisions\/13659"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media\/3421"}],"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}]}}