title: How can I limit iterations of a loop in Python?
body: <p>Say I have a list of items, and I want to iterate over the first few of it:</p>
  
  <pre><code>items = list(range(10)) # I mean this to represent any kind of iterable.
  limit = 5
  </code></pre>
  
  <h2>Naive implementation</h2>
  
  <p>The Python naïf coming from other languages would probably write this perfectly serviceable and performant (if unidiomatic) code:</p>
  
  <pre><code>index = 0
  for item in items: # Python's `for` loop is a for-each.
      print(item)    # or whatever function of that item.
      index += 1
      if index == limit:
          break
  </code></pre>
  
  <h2>More idiomatic implementation</h2>
  
  <p>But Python has enumerate, which subsumes about half of that code nicely:</p>
  
  <pre><code>for index, item in enumerate(items):
      print(item)
      if index == limit: # There's gotta be a better way.
          break
  </code></pre>
  
  <p>So we've about cut the extra code in half. But there's gotta be a better way.</p>
  
  <h1>Can we approximate the below pseudocode behavior?</h1>
  
  <p>If enumerate took another optional <code>stop</code> argument (for example, it takes a <code>start</code> argument like this: <code>enumerate(items, start=1)</code>) that would, I think, be ideal, but the below doesn't exist (see the <a href="https://docs.python.org/3/library/functions.html#enumerate" rel="noreferrer">documentation on enumerate here</a>):</p>
  
  <pre><code># hypothetical code, not implemented:
  for _, item in enumerate(items, start=0, stop=limit): # `stop` not implemented
      print(item)
  </code></pre>
  
  <p>Note that there would be no need to name the <code>index</code> because there is no need to reference it.</p>
  
  <p>Is there an idiomatic way to write the above? How?</p>
  
  <p>A secondary question: why isn't this built into enumerate?</p>