{"id":1156,"date":"2021-05-30T14:40:15","date_gmt":"2021-05-30T14:40:15","guid":{"rendered":"https:\/\/www.ishygddt.xyz\/~blog\/?p=1156"},"modified":"2022-03-08T19:01:11","modified_gmt":"2022-03-08T19:01:11","slug":"python-nested-in-line-for-statements","status":"publish","type":"post","link":"http:\/\/www.ishygddt.xyz\/~blog\/2021\/05\/python-nested-in-line-for-statements","title":{"rendered":"Python: Nested in-line \"for\" statements"},"content":{"rendered":"<p>I'll explain with an example.<\/p>\n<p>Let's say that you've got some <em>existing<\/em> nested for-loop that you want to inline:<\/p>\n<pre><code class=\"language-python\" data-line=\"\">for bytestring in x:\n\tfor i in bytestring:\n\t\tfor k in reversed(range(8)):\n\t\t\tyield ((i &amp; (0b1 &lt;&lt; k)) &gt;&gt; k)<\/code><\/pre>\n<p>Intuitively, you might think that you would type something like \"bit for k in bits for bits in in bytes for bytes in x\" \u2014 but this is wrong.<\/p>\n<p>Instead, you must leave the <code class=\"language-python\" data-line=\"\">for<\/code>s in the <strong>same order as in the code-block form<\/strong>, only popping the <em>inner expression itself<\/em> to the front:<\/p>\n<pre><code class=\"language-python\" data-line=\"\">iter(  ((i &amp; (0b1 &lt;&lt; k)) &gt;&gt; k) for bytestring in x for i in bytestring for k in reversed(range(8))  )<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>You must leave the <code class=\"language-python\" data-line=\"\">for<\/code>s in the <strong>same order as in the code-block form<\/strong>, only popping the <em>inner expression itself<\/em> to the front:\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":[63,64,144,44],"class_list":["post-1156","post","type-post","status-publish","format-standard","hentry","category-writeups","tag-bit-manipulation","tag-loops","tag-programming","tag-python"],"_links":{"self":[{"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/posts\/1156","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=1156"}],"version-history":[{"count":7,"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/posts\/1156\/revisions"}],"predecessor-version":[{"id":2006,"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/posts\/1156\/revisions\/2006"}],"wp:attachment":[{"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/media?parent=1156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/categories?post=1156"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/tags?post=1156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}