{"id":981,"date":"2021-05-07T06:36:15","date_gmt":"2021-05-07T06:36:15","guid":{"rendered":"http:\/\/www.ishygddt.xyz\/~blog\/?p=981"},"modified":"2022-03-08T14:03:25","modified_gmt":"2022-03-08T20:03:25","slug":"breaking-out-of-multiple-for-loops-in-python","status":"publish","type":"post","link":"http:\/\/www.ishygddt.xyz\/~blog\/2021\/05\/breaking-out-of-multiple-for-loops-in-python","title":{"rendered":"Breaking out of multiple \"for\" loops in Python"},"content":{"rendered":"<p>Let's say you have some nested <code class=\"language-python\" data-line=\"\">for<\/code>-loop that you want to break out of <em>from the inner loop<\/em>:<\/p>\n<pre><code class=\"language-python\" data-line=\"\">x = default\nfor key, values in dict_of_sets:\u00a0\n\tfor value in values:\u00a0\n\t\tif predicate(value):\u00a0\n\t\t\tx = key\u00a0\n\t\t\tbreak 2  # INVALID SYNTAX, SADLY...<\/code><\/pre>\n<p>Well, it turns out that <a href=\"https:\/\/docs.python.org\/3\/reference\/compound_stmts.html#the-for-statement\">Python supports <code class=\"language-python\" data-line=\"\">for-else<\/code><\/a>, so this can be made trivial:<\/p>\n<pre><code class=\"language-python\" data-line=\"\">x = default_value\u00a0\nfor key, values in dict_of_sets:\u00a0\n\tfor value in values:\u00a0\n\t\tif predicate(key, value):\u00a0\n\t\t\tx = value\u00a0\n\t\t\tbreak  # THIS BREAKS BOTH LOOPS!\u00a0\n\telse:\u00a0\n\t\tcontinue\u00a0\n\tbreak<\/code><\/pre>\n<p><span style=\"text-decoration: underline\">You don't need to know why this works to use it<\/span>; but, if you do, here's how it works:<\/p>\n<ul>\n<li>When the inner <code class=\"language-python\" data-line=\"\">for<\/code>-loop <strong>exhausts<\/strong>, its <code class=\"language-python\" data-line=\"\">else<\/code>-clause executes next, which <code class=\"language-python\" data-line=\"\">continue<\/code>s the program through that iteration of the <em>outer<\/em> <code class=\"language-python\" data-line=\"\">for<\/code>-loop (quietly skipping the <code class=\"language-python\" data-line=\"\">break<\/code> statement on line 9).<\/li>\n<li>If the inner <code class=\"language-python\" data-line=\"\">for<\/code>-loop is, instead, <strong>killed<\/strong> with the <code class=\"language-python\" data-line=\"\">break<\/code> on line 6, its <code class=\"language-python\" data-line=\"\">else<\/code>-clause is <strong>skipped<\/strong>, allowing the <code class=\"language-python\" data-line=\"\">break<\/code> statement on line 9 to execute and terminate the outer <code class=\"language-python\" data-line=\"\">for<\/code>-loop.<\/li>\n<\/ul>\n<p>This can be used as much as you like:<\/p>\n<pre><code class=\"language-python\" data-line=\"\">for i in range(3):\u00a0\n  for j in range(3):\u00a0\n    for k in range(3):\u00a0\n      for l in range(3):\u00a0\n        for m in range(3):\u00a0\n          for n in range(3):\u00a0\n            print(i, j, k, l, m, n)\u00a0\n            if input(&quot;Press enter to continue, or type anything to break out of all these loops.\\n&gt; &quot;):\u00a0\n              break  # THIS WILL BREAK ALL OF THE LOOPS!\u00a0\n          else:\u00a0\n            continue\u00a0\n          break\u00a0\n        else:\u00a0\n          continue\u00a0\n        break\u00a0\n      else:\u00a0\n        continue\u00a0\n      break\u00a0\n    else:\u00a0\n      continue\u00a0\n    break\u00a0\n  else:\u00a0\n    continue\u00a0\n  break<\/code><\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python doesn't support <code class=\"language-javascript\" data-line=\"\">break some_label;<\/code> or <code class=\"language-c\" data-line=\"\">break 3;<\/code>, but it <em>does<\/em> support <code class=\"language-python\" data-line=\"\">for-else<\/code>\u2026<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[64,144,44],"class_list":["post-981","post","type-post","status-publish","format-standard","hentry","category-writeups","tag-loops","tag-programming","tag-python"],"_links":{"self":[{"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/posts\/981","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/comments?post=981"}],"version-history":[{"count":9,"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/posts\/981\/revisions"}],"predecessor-version":[{"id":2087,"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/posts\/981\/revisions\/2087"}],"wp:attachment":[{"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/media?parent=981"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/categories?post=981"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/tags?post=981"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}