<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
> <channel><title>Autarchy of the Private Cave &#187; looping</title> <atom:link href="https://bogdan.org.ua/tags/looping/feed" rel="self" type="application/rss+xml" /><link>https://bogdan.org.ua</link> <description>Tiny bits of bioinformatics, [web-]programming etc</description> <lastBuildDate>Wed, 28 Dec 2022 16:09:04 +0000</lastBuildDate> <language>en-US</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>https://wordpress.org/?v=3.8.27</generator> <item><title>Useful Python looping techniques</title><link>https://bogdan.org.ua/2007/09/26/useful-python-looping-techniques.html</link> <comments>https://bogdan.org.ua/2007/09/26/useful-python-looping-techniques.html#comments</comments> <pubDate>Wed, 26 Sep 2007 09:21:00 +0000</pubDate> <dc:creator><![CDATA[Bogdan]]></dc:creator> <category><![CDATA[Programming]]></category> <category><![CDATA[Python]]></category> <category><![CDATA[example]]></category> <category><![CDATA[how-to]]></category> <category><![CDATA[looping]]></category> <guid
isPermaLink="false">http://bogdan.org.ua/2007/09/26/useful-python-looping-techniques.html</guid> <description><![CDATA[These are all excerpts from the Python documentation. To synchronously and simultaneously loop over two sequences: questions = &#91;'name', 'quest', 'favourite colour'&#93; answers = &#91;'Lancelot', 'the holy grail', 'blue'&#93; &#160; for q, a in zip&#40;questions, answers&#41;: &#160; &#160; print 'What is your %s? &#160;It is %s.' % &#40;q, a&#41; To loop over a sequence with [&#8230;]]]></description> <content:encoded><![CDATA[<p>These are all excerpts from the Python documentation.</p><p>To synchronously and simultaneously loop over two sequences:</p><div
id="ig-sh-1" class="syntax_hilite"><div
class="code"><ol
class="python" style="font-family:monospace;"><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">questions <span
style="color: #66cc66;">=</span> <span
style="color: black;">&#91;</span><span
style="color: #483d8b;">'name'</span><span
style="color: #66cc66;">,</span> <span
style="color: #483d8b;">'quest'</span><span
style="color: #66cc66;">,</span> <span
style="color: #483d8b;">'favourite colour'</span><span
style="color: black;">&#93;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">answers <span
style="color: #66cc66;">=</span> <span
style="color: black;">&#91;</span><span
style="color: #483d8b;">'Lancelot'</span><span
style="color: #66cc66;">,</span> <span
style="color: #483d8b;">'the holy grail'</span><span
style="color: #66cc66;">,</span> <span
style="color: #483d8b;">'blue'</span><span
style="color: black;">&#93;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #ff7700;font-weight:bold;">for</span> q<span
style="color: #66cc66;">,</span> a <span
style="color: #ff7700;font-weight:bold;">in</span> <span
style="color: #008000;">zip</span><span
style="color: black;">&#40;</span>questions<span
style="color: #66cc66;">,</span> answers<span
style="color: black;">&#41;</span>:</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">print</span> <span
style="color: #483d8b;">'What is your %s? &nbsp;It is %s.'</span> % <span
style="color: black;">&#40;</span>q<span
style="color: #66cc66;">,</span> a<span
style="color: black;">&#41;</span></div></li></ol></div></div><p>To loop over a sequence with both key and value:<br
/> <span
id="more-229"></span></p><div
id="ig-sh-2" class="syntax_hilite"><div
class="code"><ol
class="python" style="font-family:monospace;"><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #ff7700;font-weight:bold;">for</span> i<span
style="color: #66cc66;">,</span> v <span
style="color: #ff7700;font-weight:bold;">in</span> <span
style="color: #008000;">enumerate</span><span
style="color: black;">&#40;</span><span
style="color: black;">&#91;</span><span
style="color: #483d8b;">'tic'</span><span
style="color: #66cc66;">,</span> <span
style="color: #483d8b;">'tac'</span><span
style="color: #66cc66;">,</span> <span
style="color: #483d8b;">'toe'</span><span
style="color: black;">&#93;</span><span
style="color: black;">&#41;</span>:</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">print</span> i<span
style="color: #66cc66;">,</span> v</div></li></ol></div></div><p>To loop over dictionary, again with key and value:</p><div
id="ig-sh-3" class="syntax_hilite"><div
class="code"><ol
class="python" style="font-family:monospace;"><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">knights <span
style="color: #66cc66;">=</span> <span
style="color: black;">&#123;</span><span
style="color: #483d8b;">'gallahad'</span>: <span
style="color: #483d8b;">'the pure'</span><span
style="color: #66cc66;">,</span> <span
style="color: #483d8b;">'robin'</span>: <span
style="color: #483d8b;">'the brave'</span><span
style="color: black;">&#125;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #ff7700;font-weight:bold;">for</span> k<span
style="color: #66cc66;">,</span> v <span
style="color: #ff7700;font-weight:bold;">in</span> knights.<span
style="color: black;">iteritems</span><span
style="color: black;">&#40;</span><span
style="color: black;">&#41;</span>:</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">print</span> k<span
style="color: #66cc66;">,</span> v</div></li></ol></div></div><p>To loop over some sorted sequence, but without modifying the original sequence (beware the use of set() in this example):</p><div
id="ig-sh-4" class="syntax_hilite"><div
class="code"><ol
class="python" style="font-family:monospace;"><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">basket <span
style="color: #66cc66;">=</span> <span
style="color: black;">&#91;</span><span
style="color: #483d8b;">'apple'</span><span
style="color: #66cc66;">,</span> <span
style="color: #483d8b;">'orange'</span><span
style="color: #66cc66;">,</span> <span
style="color: #483d8b;">'apple'</span><span
style="color: #66cc66;">,</span> <span
style="color: #483d8b;">'pear'</span><span
style="color: #66cc66;">,</span> <span
style="color: #483d8b;">'orange'</span><span
style="color: #66cc66;">,</span> <span
style="color: #483d8b;">'banana'</span><span
style="color: black;">&#93;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #ff7700;font-weight:bold;">for</span> f <span
style="color: #ff7700;font-weight:bold;">in</span> <span
style="color: #008000;">sorted</span><span
style="color: black;">&#40;</span><span
style="color: #008000;">set</span><span
style="color: black;">&#40;</span>basket<span
style="color: black;">&#41;</span><span
style="color: black;">&#41;</span>:</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">print</span> f</div></li></ol></div></div><p>For looping sorted dictionaries, see also <a
href="http://bogdan.org.ua/2007/09/26/how-to-sort-python-dict-dictionary.html">How to sort Python dict (dictionary)</a>.</p><p><a
class="a2a_button_citeulike" href="https://www.addtoany.com/add_to/citeulike?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F09%2F26%2Fuseful-python-looping-techniques.html&amp;linkname=Useful%20Python%20looping%20techniques" title="CiteULike" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F09%2F26%2Fuseful-python-looping-techniques.html&amp;linkname=Useful%20Python%20looping%20techniques" title="Pocket" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_kindle_it" href="https://www.addtoany.com/add_to/kindle_it?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F09%2F26%2Fuseful-python-looping-techniques.html&amp;linkname=Useful%20Python%20looping%20techniques" title="Kindle It" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_evernote" href="https://www.addtoany.com/add_to/evernote?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F09%2F26%2Fuseful-python-looping-techniques.html&amp;linkname=Useful%20Python%20looping%20techniques" title="Evernote" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2007%2F09%2F26%2Fuseful-python-looping-techniques.html&amp;linkname=Useful%20Python%20looping%20techniques" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a
class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fbogdan.org.ua%2F2007%2F09%2F26%2Fuseful-python-looping-techniques.html&#038;title=Useful%20Python%20looping%20techniques" data-a2a-url="https://bogdan.org.ua/2007/09/26/useful-python-looping-techniques.html" data-a2a-title="Useful Python looping techniques"><img
src="https://static.addtoany.com/buttons/share_save_120_16.png" alt="Share"></a></p>]]></content:encoded> <wfw:commentRss>https://bogdan.org.ua/2007/09/26/useful-python-looping-techniques.html/feed</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss>