{"id":59246,"date":"2022-03-09T13:00:56","date_gmt":"2022-03-09T12:00:56","guid":{"rendered":"https:\/\/eiposgrados.com\/?p=59246"},"modified":"2022-04-06T09:55:04","modified_gmt":"2022-04-06T07:55:04","slug":"improving-our-python-code-an-alternative-to-range","status":"publish","type":"post","link":"https:\/\/eiposgrados.com\/eng\/python-blog\/improving-our-python-code-an-alternative-to-range\/","title":{"rendered":"Improving our Python code: an alternative to range"},"content":{"rendered":"<h2 class=\"gb-headline gb-headline-23fb5f3d gb-headline-text\">range function<\/h2>\n\n\n\n<p>The known <strong>Python range function<\/strong>, which they taught us in our programming beginnings to use in a <strong>for loop<\/strong>, it is possible that in many cases we can replace it with better options and with cleaner and more modern code. This function has its origin in the C\/C++\/Java languages, predecessors of this programming language, and they really need an index loop variable, but in this programming language it is not necessary to have this index.\u00a0<\/p>\n\n\n\n<p>The loop for an iterable sequence usually looks like this:<\/p>\n\n\n\n<p>for x in range(len(seq)):<\/p>\n\n\n\n<p>#haz_something_with (seq[x])<\/p>\n\n\n\n<p>The variable x is used only to access the next element from the iterable. Since Python \u201c<em>for<\/em>&quot;is a loop&quot;<em>foreach<\/em>\u201d, I believe, that there is a better way to do this; iterating directly over the iterable (hence the name) and it would look like this:<\/p>\n\n\n\n<p><em>for<\/em> thing in sequence:<\/p>\n\n\n\n<p>#HDo_something_with (thing)<\/p>\n\n\n\n<p>&nbsp;Which in a simple case would look like this:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img fetchpriority=\"high\" decoding=\"async\" src=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2022\/03\/captura-de-pantalla-2022-03-09-a-las-122402.png\" alt=\"Screenshot 2022 03 09 At 12.24.02\" class=\"wp-image-59247\" width=\"903\" height=\"78\" title=\"\" srcset=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2022\/03\/captura-de-pantalla-2022-03-09-a-las-122402.png 910w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2022\/03\/captura-de-pantalla-2022-03-09-a-las-122402-300x26.png 300w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2022\/03\/captura-de-pantalla-2022-03-09-a-las-122402-768x66.png 768w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2022\/03\/captura-de-pantalla-2022-03-09-a-las-122402-123x11.png 123w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2022\/03\/captura-de-pantalla-2022-03-09-a-las-122402-200x17.png 200w\" sizes=\"(max-width: 903px) 100vw, 903px\" \/><\/figure>\n\n\n\n<p>and would give the expected result for each value in the iteration and using the value defined in the sequence:<\/p>\n\n\n\n<p>&gt;&gt;27<\/p>\n\n\n\n<p>&gt;&gt;64<\/p>\n\n\n\n<p>This way of <strong>direct iteration<\/strong> It&#039;s about 30% faster, doesn&#039;t require creating the indexes, and is easier to read the code (\u201cfor [each] element in [that] sequence, do something with [that element].&nbsp;<\/p>\n\n\n\n<h2 class=\"gb-headline gb-headline-87a8ba99 gb-headline-text\">Enumerate function<\/h2>\n\n\n\n<p>To modify each element in a <strong>mutable iterable<\/strong>, we also have no problem for not having an index. In that case we have another function available: <strong>\u201cenumerate yourself\u201d<\/strong>, which returns a generator that produces tuples of elements and their respective indices in the form (x,seq[x]).&nbsp;<\/p>\n\n\n\n<p>Let&#039;s look at the code of a very simple case:<\/p>\n\n\n\n<p>for x, thing in enumerate(seq):<\/p>\n\n\n\n<p>seq[x] = do_something_with(item)<\/p>\n\n\n\n<p>We are going to do it with a simple case, where what we do is cube it, taking into account that we use a string for the iteration and therefore the index starts at 0 and the iteration numbers come and it is similar to the previous example:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"864\" height=\"76\" src=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2022\/03\/captura-de-pantalla-2022-03-09-a-las-122550.png\" alt=\"Screenshot 2022 03 09 At 12.25.50\" class=\"wp-image-59248\" title=\"\" srcset=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2022\/03\/captura-de-pantalla-2022-03-09-a-las-122550.png 864w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2022\/03\/captura-de-pantalla-2022-03-09-a-las-122550-300x26.png 300w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2022\/03\/captura-de-pantalla-2022-03-09-a-las-122550-768x68.png 768w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2022\/03\/captura-de-pantalla-2022-03-09-a-las-122550-123x11.png 123w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2022\/03\/captura-de-pantalla-2022-03-09-a-las-122550-200x18.png 200w\" sizes=\"(max-width: 864px) 100vw, 864px\" \/><\/figure>\n\n\n\n<p>We obtain the index and the following result:<\/p>\n\n\n\n<p>0 0<\/p>\n\n\n\n<p>1 1<\/p>\n\n\n\n<p>2 8<\/p>\n\n\n\n<p>3 27<\/p>\n\n\n\n<p>The function &quot;<em>enumerate<\/em>\u201d gives us many advantages and can be used for various processes, which we are not going to detail here and I encourage you to explore.<\/p>\n\n\n\n<p>It may be thought that it requires<em>range<\/em>\u201d to manipulate <strong>parallel iterables<\/strong>, such as coordinates. Let&#039;s look at an example, where the strings X and Y store the coordinates of two-dimensional points.&nbsp;<\/p>\n\n\n\n<p>In this case, if we write the code like this:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"908\" height=\"56\" src=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2022\/03\/captura-de-pantalla-2022-03-09-a-las-123101.png\" alt=\"Screenshot 2022 03 09 At 12.31.01\" class=\"wp-image-59251\" title=\"\" srcset=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2022\/03\/captura-de-pantalla-2022-03-09-a-las-123101.png 908w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2022\/03\/captura-de-pantalla-2022-03-09-a-las-123101-300x19.png 300w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2022\/03\/captura-de-pantalla-2022-03-09-a-las-123101-768x47.png 768w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2022\/03\/captura-de-pantalla-2022-03-09-a-las-123101-123x8.png 123w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2022\/03\/captura-de-pantalla-2022-03-09-a-las-123101-200x12.png 200w\" sizes=\"(max-width: 908px) 100vw, 908px\" \/><\/figure>\n\n\n\n<p>We obtain a coordinate system whose result is:<\/p>\n\n\n\n<p>1 B<\/p>\n\n\n\n<p>2 a<\/p>\n\n\n\n<p>If we want to go deeper we have other possibilities, like this other simple example, in which we use tuples:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"910\" height=\"132\" src=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2022\/03\/captura-de-pantalla-2022-03-09-a-las-123432.png\" alt=\"Screenshot 2022 03 09 At 12.34.32\" class=\"wp-image-59256\" title=\"\" srcset=\"https:\/\/eiposgrados.com\/wp-content\/uploads\/2022\/03\/captura-de-pantalla-2022-03-09-a-las-123432.png 910w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2022\/03\/captura-de-pantalla-2022-03-09-a-las-123432-300x44.png 300w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2022\/03\/captura-de-pantalla-2022-03-09-a-las-123432-768x111.png 768w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2022\/03\/captura-de-pantalla-2022-03-09-a-las-123432-123x18.png 123w, https:\/\/eiposgrados.com\/wp-content\/uploads\/2022\/03\/captura-de-pantalla-2022-03-09-a-las-123432-200x29.png 200w\" sizes=\"(max-width: 910px) 100vw, 910px\" \/><\/figure>\n\n\n\n<p>We get a result like this:<\/p>\n\n\n\n<p>(&#039;Name&#039;, &#039;John&#039;)<\/p>\n\n\n\n<p>(&#039;First surname&#039;, &#039;Lopez&#039;)<\/p>\n\n\n\n<p>(&#039;Middle last name&#039;, &#039;Garcia&#039;)<\/p>\n\n\n\n<p>We see that <strong>Python allows us multiple ways to do the same thing<\/strong> and referring to the zen of Python (import this) we can say: \u201cNow is better than never.\u201d<\/p>","protected":false},"excerpt":{"rendered":"<p>If you are a Python developer, this post interests you. In it we will discuss an improvement to our Python code: an alternative to range.<\/p>","protected":false},"author":90,"featured_media":59262,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[407,142],"tags":[],"class_list":["post-59246","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog-python","category-blog"],"acf":[],"_links":{"self":[{"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/posts\/59246","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/users\/90"}],"replies":[{"embeddable":true,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/comments?post=59246"}],"version-history":[{"count":0,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/posts\/59246\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/media\/59262"}],"wp:attachment":[{"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/media?parent=59246"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/categories?post=59246"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eiposgrados.com\/eng\/wp-json\/wp\/v2\/tags?post=59246"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}