{"id":29,"date":"2009-01-15T13:27:01","date_gmt":"2009-01-15T20:27:01","guid":{"rendered":"http:\/\/www.reisun.com\/?p=29"},"modified":"2009-01-15T13:27:01","modified_gmt":"2009-01-15T20:27:01","slug":"qn-python-looping-through-sequence-and-indices-simultaneously","status":"publish","type":"post","link":"https:\/\/www.reisun.com\/?p=29","title":{"rendered":"QN: Python: Looping through sequence and indices simultaneously"},"content":{"rendered":"<p>I started programming in python from the days of 1.5.2, and have not kept up to date with some of the new features. Two features that caught me by surprise:<\/p>\n<pre>enumerate()\u00c2\u00a0 # builtin function<\/pre>\n<pre>\"...\".format() # String method<\/pre>\n<p>enumerate() takes a list an returns an interator tuple (index,value) as it goes along the sequence. Very handy and obviates the need for the very messy:<\/p>\n<pre>for i in range(len(L)):<\/pre>\n<pre>\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 print \"I need the index {0} and the item {1} at the same time\".format(i,L[i])<\/pre>\n<p>This illustrates the second new feature I just realized, the .format() method. This apparently replaces the &#8216;%&#8217; operator and allows formatted strings to include positional replacement strings using {idx} notation.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I started programming in python from the days of 1.5.2, and have not kept up to date with some of the new features. Two features that caught me by surprise: enumerate()\u00c2\u00a0 # builtin function &#8220;&#8230;&#8221;.format() # String method enumerate() takes a list an returns an interator tuple (index,value) as it goes along the sequence. Very [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,14],"tags":[],"class_list":["post-29","post","type-post","status-publish","format-standard","hentry","category-python","category-quicknote"],"_links":{"self":[{"href":"https:\/\/www.reisun.com\/index.php?rest_route=\/wp\/v2\/posts\/29","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.reisun.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.reisun.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.reisun.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.reisun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=29"}],"version-history":[{"count":0,"href":"https:\/\/www.reisun.com\/index.php?rest_route=\/wp\/v2\/posts\/29\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.reisun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=29"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.reisun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=29"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.reisun.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=29"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}