title: Python how to store last element in yield iterator?
body: <p>I have the following code:</p>
  <pre><code>import zlib
  
  
  def inflate(infile):
      data = infile.read()
      offset = 0
      while offset &lt; len(data):
          window = data[offset : offset + 2]
          for key, value in zlib_headers.items():
              if window == key:
                  decomp_obj = zlib.decompressobj()
                  yield key, offset, decomp_obj.decompress(data[offset:])
          if offset == len(data):
              break
          offset += 1
  
  
  if __name__ == &quot;__main__&quot;:
      zlib_headers = {b&quot;\x78\x01&quot;: 3, b&quot;\x78\x9c&quot;: 6, b&quot;\x78\xda&quot;: 9}
  
      with open(&quot;input_file&quot;, &quot;rb&quot;) as infile:
          *_, last = inflate(infile)
  
      key, offset, data = last
      start_offset = offset + len(zlib.compress(data, zlib_headers[key]))
  
      print(start_offset)
  </code></pre>
  <p>Below the function, I'm printing information generated from the iterator. As you can see, I'm doing so for each one, but I only need to do that for the last one. How can I do this? I still need the function to work as a generator using &quot;yield.&quot;</p>