Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

4
  • 2
    But note that iterable[-1] will not work to all iterables (such as generator that do not have len) Commented Oct 27, 2009 at 12:23
  • If all you want is to access the last item after the loop, simply use item instead of re-calculating it using list[-1]. But nevertheless: I don't think this is what the OP was asking for, was it? Commented Oct 27, 2009 at 12:27
  • 1
    Re: iterable.__iter__() - please don't call __ functions directly. Should be iter(iterable). Commented Oct 28, 2009 at 2:16
  • In 3.x, iterator.next() should instead be next(iterator). However, this is really unpythonic. Digging into the details of the iterator protocol shouldn't be necessary for this task - for loops exist specifically so that code like this isn't necessary for normal cases. Commented Mar 29, 2023 at 7:15