<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Esteban S.</title>
    <description>The latest articles on DEV Community by Esteban S. (@esteban03).</description>
    <link>https://dev.to/esteban03</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F321582%2F13751ece-d33f-41f6-9e67-ef0bdd0e46ba.jpeg</url>
      <title>DEV Community: Esteban S.</title>
      <link>https://dev.to/esteban03</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/esteban03"/>
    <language>en</language>
    <item>
      <title>How to easily know when we are at the last element inside a loop</title>
      <dc:creator>Esteban S.</dc:creator>
      <pubDate>Mon, 28 Feb 2022 14:30:30 +0000</pubDate>
      <link>https://dev.to/esteban03/how-to-easily-know-when-we-are-at-the-last-element-inside-a-loop-f6f</link>
      <guid>https://dev.to/esteban03/how-to-easily-know-when-we-are-at-the-last-element-inside-a-loop-f6f</guid>
      <description>&lt;p&gt;An easy way to detect if we are at the last element of a python vector&lt;/p&gt;

&lt;p&gt;First, we will create a function that will return a generator using the yield keyword.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from typing import Union, Set, List, Tuple, Any

Vectors = Union[Tuple, List, Set]

def lookthelast(items: Vectors) -&amp;gt; Tuple[Any, bool]:
    lenght_items = len(items)

    for index, value in enumerate(items):
        if index == (lenght_items - 1):
            yield value, True; break

        yield value, False
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;you can do this directly in your use case, but if we encapsulate this, we improve readability. it only imports the function and we avoid rewriting this code every loop.&lt;/p&gt;

&lt;p&gt;An use case example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;numbers = ["one", "two", "three"]

for value, is_the_last in lookthelast(numbers):
    if is_the_last:
        print("this is the last element -&amp;gt; " + value)
        break

    print(value)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;👉 execute this example &lt;a href="https://www.mycompiler.io/view/By8kIcoAX9R"&gt;here&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>shortpost</category>
    </item>
  </channel>
</rss>
