{"id":19157,"date":"2020-11-15T04:46:56","date_gmt":"2020-11-15T12:46:56","guid":{"rendered":"https:\/\/careerkarma.com\/blog\/?p=19157"},"modified":"2023-12-01T04:04:03","modified_gmt":"2023-12-01T12:04:03","slug":"binary-search-python","status":"publish","type":"post","link":"https:\/\/careerkarma.com\/blog\/binary-search-python\/","title":{"rendered":"Binary Search Python: A Step-by-Step Guide"},"content":{"rendered":"\n<p><em>A Python binary search finds the position of an item in a sorted array. It divides a list in half. If a specified value is higher than the middle number, the search focuses on the right of the list. Otherwise, the search looks for the number on the left of the list.<\/em><\/p>\n\n\n\n<p>How do you find the position of an item in a list? Binary searches have your back on that one. By using binary searches, you can easily find the position of an element inside a sorted array.<\/p>\n\n\n\n<p>Computers are good at searching through lists to find a specific item. The rules that computers use to find items in a list are called search algorithms. One of the most popular <a href=\"https:\/\/careerkarma.com\/blog\/python-algorithms\/\">Python algorithms<\/a> is the binary search.<\/p>\n\n\n\n<p>In this guide, we\u2019re going to discuss what binary searches are and how they work. We\u2019ll walk through an example of a binary search in Python so that you can learn how to write one into your programs. Let\u2019s get started!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is a Python Binary Search?<\/h2>\n\n\n\n<p>A Python binary search is an algorithm that finds the position of an element in an ordered array. Binary searches repeatedly divide a list into two halves. Then, a search compares if a value is higher or lower than the middle value in the list.<\/p>\n\n\n\n<p>There are two ways you can perform a binary search. Both approaches set pointers which are used to track the highest and lowest positions at a particular point in an array.<\/p>\n\n\n\n<p>The first approach you can use is the iterative method. In this approach, a set of statements are repeated to determine the position of an element in an array. In Python, we use a <em>while<\/em> loop for this purpose.<\/p>\n\n\n\n<p>The other approach is the recursive method. This is where you write a function that calls itself again and again until an element in a list is found. The recursive method uses the divide and conquer approach that we discussed earlier and repeats the process of searching until an element is found.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Binary Search Tree Python: Step-by-Step<\/h2>\n\n\n\n<p>With all that talk about dividing and conquering and recursion, it can be easy to lose track of exactly how a binary search works. For that reason, we\u2019re going to jump straight into an example of how the binary search works. Consider the following list:<\/p>\n\n\n\n<table class=\"wp-block-table course-info-table\"><tbody><tr><td>7<\/td><td>9<\/td><td>14<\/td><td>22<\/td><td>34<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>We\u2019re going to search for the number \u201c22\u201d in our list.<\/p>\n\n\n\n<p>To start, we are going to set two pointers in our lists. One pointer will reflect the highest value in the list, and the other point will reflect the lowest value:<\/p>\n\n\n\n<table class=\"wp-block-table course-info-table\"><tbody><tr><td>Low<\/td><td><br><\/td><td><br><\/td><td><br><\/td><td>High<\/td><\/tr><tr><td>7<\/td><td>9<\/td><td>14<\/td><td>22<\/td><td>34<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>Our next step is to find the middle element in the array, which is 14. If this value is equal to the value for which we are searching, then this value should be returned.<\/p>\n\n\n\n<p>In this case, 14 is not the same as 22. So, our program needs to perform a comparison.<\/p>\n\n\n\n<p>We are going to compare the number for which we are searching with the middle element of the elements on the right side of the current middle element. We&#8217;ll only do this if the number for which we are searching is greater than the middle number. Otherwise, we\u2019ll compare the element with the middle element on the left side of the current middle element.<\/p>\n\n\n\n<p>The number 22 is greater than 14. Our program starts to compare 22 to the middle element on the right side of our current middle element (14). In this case, that number is 22. This is equal to the number for which we are searching.<\/p>\n\n\n\n<table class=\"wp-block-table course-info-table\"><tbody><tr><td><br><\/td><td><br><\/td><td>Low<\/td><td>Middle<\/td><td>High<\/td><\/tr><tr><td>7<\/td><td>9<\/td><td>14<\/td><td><strong>22<\/strong><\/td><td>34<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>We\u2019ve found our middle value. Our program will now return the index position of that number. In this case, the index position of 22 is 3 (remember, lists are indexed starting at 0!).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to Implement a Binary Search in Python<\/h2>\n\n\n\n<p>Let\u2019s get our hands dirty with some Python code. We\u2019re going to explore a Python implementation of a binary search using both the approaches that we discussed earlier: the iterative and recursive methods.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Iterative Binary Search&nbsp;in Python<\/h3>\n\n\n\n<p>We\u2019ll begin with the iterative method. This is where we\u2019ll loop through every item in our list. Then, we&#8217;ll find the middle value in the list. We will continue to do so until we have found the value for which we are looking.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Binary Search Function<\/h4>\n\n\n\n<p>Let\u2019s start by defining a <a href=\"https:\/\/careerkarma.com\/blog\/python-functions\/\">Python function<\/a> for our binary search:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>def findValue(numbers, number_to_find):\n\tlow = 0\n\thigh = len(listnumbers - 1\n\n\twhile low &lt;= high:\n\t\tmiddle = low + (high - low) \/\/ 2\n\n\t\tif numbers[middle] == number_to_find:\n\t\t\treturn middle\n\t\telif numbers[middle] &lt; number_to_find:\n\t\t\tlow = middle + 1\n\t\telse:\n\t\t\thigh = middle - 1\n\treturn -1\n<\/pre><\/div>\n\n\n\n<p>Our function accepts two parameters: the list through which we want to search, and the number that we want to find in our list.<\/p>\n\n\n\n<p>We\u2019ve then declared two <a href=\"https:\/\/careerkarma.com\/blog\/python-variables\/\">Python variables<\/a> that store the default values for the lowest and highest values in the list. <em>low<\/em> is set to 0, which is the starting index value in the list. <em>high<\/em> is set to the length of the list minus one (because lists are indexed from zero).<\/p>\n\n\n\n<p>Our next step was to declare a <a href=\"https:\/\/careerkarma.com\/blog\/do-while-python\/\">Python while loop<\/a>. This loop runs while the lowest element is smaller than or equal to the highest number. This means that our loop will only run if our number has not yet been found.<\/p>\n\n\n\n<p>Then, we calculate the middle number. We do this by subtracting the lowest value from the highest value. We then calculate the modulo (remainder) of that number when divided by 2. Then, we add the modulo to the value of the lowest number.<\/p>\n\n\n\n<p>If the middle number in our list is the same as the number we want to find, we return the position of that number.<\/p>\n\n\n\n<p>We set our new \u201clow\u201d number to be equal to one greater than the middle position. This happens if the middle number is less than the number that we want to find. This moves our search down to the left side of our list, like we did in our visual example earlier.<\/p>\n\n\n\n<p>Otherwise, we set our \u201chigh\u201d number to be equal to one less than the middle position. This moves our search to the right side of our list.<\/p>\n\n\n\n<p>This is repeated until low is equal to or less than high. If our value is not found, we return -1. We\u2019ll talk about why in a minute.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Execute the Search<\/h4>\n\n\n\n<p>Add the following code to the bottom of your program, outside the <em>findValue<\/em> function:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>numbers = [7, 9, 14, 22, 34]\nnumber_to_find = 22\n\nfinal = findValue(numbers, number_to_find)\n\nif final == -1:\n\tprint(&quot;This item was not found in the list.&quot;)\nelse:\n\tprint(&quot;The number &quot; + str(number_to_find) + &quot; was found at index position &quot; + str(final) + &quot;.&quot;)\n<\/pre><\/div>\n\n\n\n<p>We\u2019ve started by declaring the list of items through which we want to search. Then we have specified the number we want to find, which is 22.<\/p>\n\n\n\n<p>Next, we call our <em>findValue()<\/em> function and pass through it the list.<\/p>\n\n\n\n<p>Here\u2019s where the -1 comes in from earlier. If the number returned by our function is -1, that means that our item was not found in the list. Programmers often use -1 in situations like this because our search function can\u2019t return a negative number.<\/p>\n\n\n\n<p>Otherwise, our program prints out a message telling us the index position of that value.<\/p>\n\n\n\n<p>Our code returns:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>The number 22 was found at index position 3.<\/pre><\/div>\n\n\n\n<p>Now we know that the number 22 appears at the index position 3.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Recursive Binary Search in Python<\/h3>\n\n\n\n<p>We can also use recursion to perform a binary search. This is where we\u2019ll define a function that keeps calling itself until a condition \u2013 our number being found \u2013 is met.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Define a Recursive Function<\/h4>\n\n\n\n<p>Like in our last example, we\u2019ll start by writing a function that performs our binary search:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>def findValue(numbers, number_to_find, low, high):\n\tif high &gt;= low:\n\t\tmiddle = low + (high - low) \/\/ 2\n\n\t\tif numbers[middle] == number_to_find:\n\t\t\treturn middle\n\t\telif numbers[middle] &lt; number_to_find:\n\t\t\treturn findValue(numbers, number_to_find, middle + 1, high)\n\t\telse:\n\t\t\treturn findValue(numbers, number_to_find, low, middle - 1)\n\t\n\telse:\n\t\treturn -1\n<\/pre><\/div>\n\n\n\n<p>Our code is somewhat similar to our last example.<\/p>\n\n\n\n<p>We start by checking whether the highest value is greater than or equal to low. If it is, our program will return -1. Otherwise, it will start performing a binary search.<\/p>\n\n\n\n<p>We calculate the middle number using the same approach as in the last example. First, we subtract the lowest from the highest value. Then, we calculate the modulo (remainder) of that value when divided by 2. Finally, we add the lowest number.<\/p>\n\n\n\n<p>We\u2019ve then written an <em>if<\/em> statement which decides how our binary search should proceed:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>If the middle number is equal to the one we are looking for, the position of that number is returned.<\/li><li>If the middle number is less than the one we are looking for, our <em>findValue()<\/em> function is called again. This time, the value of <em>low<\/em> is set to be equal to one greater than our middle value.<\/li><li>If the middle number is greater than the one we are looking for, the <em>findValue()<\/em> function is called. The value of \u201chigh\u201d will be equal to one less than the middle value.<\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">Write the Main Program<\/h4>\n\n\n\n<p>All we\u2019ve got left to do is write our main program:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>numbers = [7, 9, 14, 22, 34]\nnumber_to_find = 22\n\nfinal = findValue(numbers, number_to_find, 0, len(numbers) - 1)\n\nif final == -1:\n\tprint(&quot;This item was not found in the list.&quot;)\nelse:\n\tprint(&quot;The number &quot; + str(number_to_find) + &quot; was found at index position &quot; + str(final) + &quot;.&quot;)\n<\/pre><\/div>\n\n\n\n<p>Our main program is the same as our earlier example bar one difference. We\u2019re passing in two new parameters to our <em>findValue()<\/em> function: low and high.<\/p>\n\n\n\n<p>We need to do this because our algorithm is recursive, and we can\u2019t assign those values in our function. If we assigned the values in our function, those values would be reset every time our function executed, which would break our search algorithm.<\/p>\n\n\n\n<p>Our code returns:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>The number 22 was found at index position 3.<\/pre><\/div>\n\n\n\n<p>We\u2019ve received the same result from earlier. In this example, we\u2019ve used a recursive program instead of an iterative program to perform our binary search.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">When Should You Use a Python Binary Search?<\/h2>\n\n\n\n<p>A binary search is an efficient way of searching through a list of numbers. This search is more efficient than a linear search. This is because the binary method cuts down your search to half as soon as you find the middle of a sorted list.<\/p>\n\n\n\n<p>It\u2019s important to note that a list must be ordered numerically before it can be searched using a binary search. Before you perform a binary search, make sure your numbers are sorted in ascending order.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Binary Search in Python Complexity Breakdown<\/h2>\n\n\n\n<p>What is the complexity of a binary search? That\u2019s a good question.<\/p>\n\n\n\n<p>The binary search algorithm has a best case complexity of O(1). This occurs when the first comparison is equal to the item for which you are searching.<\/p>\n\n\n\n<p>The average and worst case complexity for a binary search is O(log n). This means that the length of time it takes to conduct a search increases logarithmically depending on how many items there are to search through in a list.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Binary searches are an efficient way to find the index position of a value in a list.<\/p>\n\n\n\n<p>Each time a binary search is run, the search will divide the list into two parts. The search focuses on the side of the list closest to the number for which you are searching.<\/p>\n\n\n\n<p>For each time the search is run, the amount of numbers through which the program needs to search is halved.<\/p>\n\n\n\n<p>To learn more about Python, read our <a href=\"https:\/\/careerkarma.com\/blog\/how-to-learn-python\/\">How to Learn Python guide<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"A Python binary search finds the position of an item in a sorted array. It divides a list in half. If a specified value is higher than the middle number, the search focuses on the right of the list. Otherwise, the search looks for the number on the left of the list. How do you&hellip;","protected":false},"author":240,"featured_media":9208,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[16578],"tags":[],"class_list":{"0":"post-19157","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-python"},"acf":{"post_sub_title":"","sprint_id":"","query_class":"Python","school_sft":"","parent_sft":"","school_privacy_policy":"","has_review":null,"is_sponser_post":"","is_guest_post":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.0 (Yoast SEO v27.0) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Binary Search Python: A Step-by-Step Guide | Career Karma<\/title>\n<meta name=\"description\" content=\"Binary searches allow you to find the position of an item in a list. On Career Karma, learn how to write a binary search algorithm in Python.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/careerkarma.com\/blog\/binary-search-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Binary Search Python: A Step-by-Step Guide\" \/>\n<meta property=\"og:description\" content=\"Binary searches allow you to find the position of an item in a list. On Career Karma, learn how to write a binary search algorithm in Python.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/careerkarma.com\/blog\/binary-search-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Career Karma\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/facebook.com\/careerkarmaapp\" \/>\n<meta property=\"article:published_time\" content=\"2020-11-15T12:46:56+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-01T12:04:03+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/12\/agence-olloweb-d9ILr-dbEdg-unsplash-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1000\" \/>\n\t<meta property=\"og:image:height\" content=\"664\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"James Gallagher\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@career_karma\" \/>\n<meta name=\"twitter:site\" content=\"@career_karma\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"James Gallagher\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/careerkarma.com\/blog\/binary-search-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/binary-search-python\/\"},\"author\":{\"name\":\"James Gallagher\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"headline\":\"Binary Search Python: A Step-by-Step Guide\",\"datePublished\":\"2020-11-15T12:46:56+00:00\",\"dateModified\":\"2023-12-01T12:04:03+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/binary-search-python\/\"},\"wordCount\":1788,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/binary-search-python\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/12\/agence-olloweb-d9ILr-dbEdg-unsplash-1.jpg\",\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/careerkarma.com\/blog\/binary-search-python\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/careerkarma.com\/blog\/binary-search-python\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/binary-search-python\/\",\"name\":\"Binary Search Python: A Step-by-Step Guide | Career Karma\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/binary-search-python\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/binary-search-python\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/12\/agence-olloweb-d9ILr-dbEdg-unsplash-1.jpg\",\"datePublished\":\"2020-11-15T12:46:56+00:00\",\"dateModified\":\"2023-12-01T12:04:03+00:00\",\"author\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"description\":\"Binary searches allow you to find the position of an item in a list. On Career Karma, learn how to write a binary search algorithm in Python.\",\"breadcrumb\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/binary-search-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/careerkarma.com\/blog\/binary-search-python\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/binary-search-python\/#primaryimage\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/12\/agence-olloweb-d9ILr-dbEdg-unsplash-1.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/12\/agence-olloweb-d9ILr-dbEdg-unsplash-1.jpg\",\"width\":1000,\"height\":664},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/careerkarma.com\/blog\/binary-search-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\/\/careerkarma.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\",\"item\":\"https:\/\/careerkarma.com\/blog\/python\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Binary Search Python: A Step-by-Step Guide\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\",\"url\":\"https:\/\/careerkarma.com\/blog\/\",\"name\":\"Career Karma\",\"description\":\"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/careerkarma.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\",\"name\":\"James Gallagher\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg\",\"caption\":\"James Gallagher\"},\"description\":\"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.\",\"url\":\"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Binary Search Python: A Step-by-Step Guide | Career Karma","description":"Binary searches allow you to find the position of an item in a list. On Career Karma, learn how to write a binary search algorithm in Python.","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:\/\/careerkarma.com\/blog\/binary-search-python\/","og_locale":"en_US","og_type":"article","og_title":"Binary Search Python: A Step-by-Step Guide","og_description":"Binary searches allow you to find the position of an item in a list. On Career Karma, learn how to write a binary search algorithm in Python.","og_url":"https:\/\/careerkarma.com\/blog\/binary-search-python\/","og_site_name":"Career Karma","article_publisher":"http:\/\/facebook.com\/careerkarmaapp","article_published_time":"2020-11-15T12:46:56+00:00","article_modified_time":"2023-12-01T12:04:03+00:00","og_image":[{"width":1000,"height":664,"url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/12\/agence-olloweb-d9ILr-dbEdg-unsplash-1.jpg","type":"image\/jpeg"}],"author":"James Gallagher","twitter_card":"summary_large_image","twitter_creator":"@career_karma","twitter_site":"@career_karma","twitter_misc":{"Written by":"James Gallagher","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/careerkarma.com\/blog\/binary-search-python\/#article","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/binary-search-python\/"},"author":{"name":"James Gallagher","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"headline":"Binary Search Python: A Step-by-Step Guide","datePublished":"2020-11-15T12:46:56+00:00","dateModified":"2023-12-01T12:04:03+00:00","mainEntityOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/binary-search-python\/"},"wordCount":1788,"commentCount":0,"image":{"@id":"https:\/\/careerkarma.com\/blog\/binary-search-python\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/12\/agence-olloweb-d9ILr-dbEdg-unsplash-1.jpg","articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/careerkarma.com\/blog\/binary-search-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/careerkarma.com\/blog\/binary-search-python\/","url":"https:\/\/careerkarma.com\/blog\/binary-search-python\/","name":"Binary Search Python: A Step-by-Step Guide | Career Karma","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/binary-search-python\/#primaryimage"},"image":{"@id":"https:\/\/careerkarma.com\/blog\/binary-search-python\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/12\/agence-olloweb-d9ILr-dbEdg-unsplash-1.jpg","datePublished":"2020-11-15T12:46:56+00:00","dateModified":"2023-12-01T12:04:03+00:00","author":{"@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"description":"Binary searches allow you to find the position of an item in a list. On Career Karma, learn how to write a binary search algorithm in Python.","breadcrumb":{"@id":"https:\/\/careerkarma.com\/blog\/binary-search-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/careerkarma.com\/blog\/binary-search-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/binary-search-python\/#primaryimage","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/12\/agence-olloweb-d9ILr-dbEdg-unsplash-1.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/12\/agence-olloweb-d9ILr-dbEdg-unsplash-1.jpg","width":1000,"height":664},{"@type":"BreadcrumbList","@id":"https:\/\/careerkarma.com\/blog\/binary-search-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/careerkarma.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Python","item":"https:\/\/careerkarma.com\/blog\/python\/"},{"@type":"ListItem","position":3,"name":"Binary Search Python: A Step-by-Step Guide"}]},{"@type":"WebSite","@id":"https:\/\/careerkarma.com\/blog\/#website","url":"https:\/\/careerkarma.com\/blog\/","name":"Career Karma","description":"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/careerkarma.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94","name":"James Gallagher","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","caption":"James Gallagher"},"description":"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.","url":"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/"}]}},"_links":{"self":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/19157","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/users\/240"}],"replies":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/comments?post=19157"}],"version-history":[{"count":0,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/19157\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media\/9208"}],"wp:attachment":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media?parent=19157"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/categories?post=19157"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/tags?post=19157"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}