Do Python iterators have a hasnext
method?

- 14,854
- 11
- 100
- 103

- 28,823
- 42
- 111
- 133
-
Related: [How do I know if a generator is empty from the start?](http://stackoverflow.com/questions/661603/how-do-i-know-if-a-generator-is-empty-from-the-start) – user2314737 May 05 '17 at 10:58
-
There is no such thing as of now. And I think, right way to do this is to add `next` and `has_next` method in iterators itself, as these methods should not be defined independently. – Shiv Krishna Jaiswal Feb 19 '23 at 03:47
-
(For context, iterators in Java and C# have a`hasNext()` as well as `Next()`. Python obviates this by throwing an exception at generator end. And the [`next(gen, default_value)`](https://docs.python.org/3/library/functions.html?highlight=next#next) idiom allows you to squelch that exception without needing `try..except`. – smci Jul 23 '23 at 04:41
18 Answers
The alternative to catching StopIteration
is to use next(iterator, default_value)
.
For example:
>>> a = iter('hi')
>>> print(next(a, None))
h
>>> print(next(a, None))
i
>>> print(next(a, None))
None
This way you can check for None
to see if you've reached the end of the iterator if you don't want to do it the exception way.
If your iterable can contain None
values you'll have to define a sentinel value and check for it instead:
>>> sentinel = object()
>>> a = iter([None, 1, 2])
>>> elem = next(a, sentinel)
>>> if elem is sentinel:
... print('end')
...
>>>

- 14,854
- 11
- 100
- 103

- 21,201
- 18
- 53
- 73
-
99if you use None as the "sentinel", you best be sure your iterator doesn't have any Nones. you could also do `sentinel = object()` and `next(iterator, sentinel)` and test with `is`. – sam boosalis Oct 03 '13 at 18:00
-
4following @samboosalis I would rather use built-in `unittest.mock.sentinel` object which allows you to write an explicit `next(a, sentinel.END_OF_ITERATION)` and then `if next(...) == sentinel.END_OF_ITERATION` – ClementWalter Dec 01 '18 at 19:40
-
11The problem is that, this way, you CONSUME the next value from the iterator as well. hasNext in Java doesn't consume the next value. – Alan Franzoni Jul 17 '20 at 12:18
No, there is no such method. The end of iteration is indicated by an exception. See the documentation.

- 32,009
- 9
- 68
- 103
-
91
-
170"It's easier to ask for forgiveness than permission.": Checking whether an iterator has a next element is not asking for permission. There are situations in which you want to test for the existence of a next element without consuming it. I would accept the try catch solution if there was an `unnext()` method to put the first element back after I have checked that it exists by calling `next()`. – Giorgio Dec 24 '12 at 20:26
-
22@Giorgio, there is no way to know whether another element exists without executing the code that generates it (you don't know whether the generator will execute `yield` or not). It is, of course, not difficult to write an adaptor that stores the result of `next()` and provides `has_next()` and `move_next()`. – avakar Dec 24 '12 at 21:10
-
7The same idea could be used to implement the `hasNext()` method (to produce, cache and return true on success, or return false on failure). Then both `hasNext()` and `next()` would depend on a common underlying `getNext()` method and cached item. I really do not see why `next()` shouldn't be in the standard library if it is so easy to implement an adaptor that provides it. – Giorgio Dec 24 '12 at 21:21
-
@Giorgio, I don't understand what you're saying. `next` is the standard; it throws on end of sequence. The adaptor could use this `next` and cache the value. Nobody is saying that it shouldn't be in the standard library; I guess there just was no demand for it. – avakar Dec 24 '12 at 21:35
-
I understand, I thought the motivation for not including it in the standard was technical. Probably, as you say, there simply was not enough demand for it. – Giorgio Dec 24 '12 at 22:09
-
@Giorgio, I mostly agree with you, although: if `hasNext()` were in the standard library, all iterators would be forced to implement it (or return an error if they don't support it ... design decisions there). And for some iterators, ones that are highly time-context-sensitive, the value of the first item from the iterator could change between the call to `hasNext()` and the call to `next()` (which could be in a different module), with the result that the `next()` returns a stale value. I don't know, just trying to see both sides of the argument. I agree it would be useful to have. – LarsH Jan 24 '13 at 22:43
-
5@LarsH: You mean e.g. an iterator that reads from a file that can be changed while reading from it? I agree that this can be a problem (which affects any library providing `next()` and `hasNext()` method, not just a hypothetical Python library). So yes, `next()` and `hasNext()` becomes tricky if the content of the stream being scanned depends on **when** elements are read. – Giorgio Jan 25 '13 at 06:42
-
@Giorgio: right, that is an example of what I was thinking of. So when Python `glob` library has `iglob()` return an iterator, we run into this problem: an iterator whose return values are time-sensitive (i.e. not a pure function). So does that mean impure functions like I/O should not be allowed as iterators? Design decisions... – LarsH Jan 25 '13 at 15:14
-
You're asking for permission either way. This isn't C ;) I guess there's no `has_next()` because can make things complicated. – sudo Mar 27 '17 at 22:00
-
1python often lacks constructs for basic language usage compelling such things as catching exceptions .. and then it becomes somehow justified as more or less the right way to do things. Another one is the lack of `do .. while` – WestCoastProjects Jun 01 '19 at 04:38
No, but you can implement your own iterable wrapper class that does:
from collections.abc import Iterator
class hn_wrapper(Iterator):
def __init__(self, it):
self.it = iter(it)
self._hasnext = None
def __iter__(self):
return self
def __next__(self):
if self._hasnext:
result = self._thenext
else:
result = next(self.it)
self._hasnext = None
return result
def hasnext(self):
if self._hasnext is None:
try:
self._thenext = next(self.it)
except StopIteration:
self._hasnext = False
else:
self._hasnext = True
return self._hasnext
Then you can use it like this:
x = hn_wrapper('ciao')
while x.hasnext():
print(next(x))
and it will emit
c
i
a
o

- 14,854
- 11
- 100
- 103

- 854,459
- 170
- 1,222
- 1,395
-
It would be a bit more pythonic to implement `hasnext()` like `next()`, i.e. renaming the method to `hn_wrapper.__hasnext__()` and then defining a top-level function that calls that method `def hasnext(iterable): return iterable.__hasnext__()`. – Boris Verkhovskiy Apr 30 '23 at 23:20
In addition to all the mentions of StopIteration
, the Python for
loop does what you want:
>>> it = iter('hello')
>>> for i in it:
... print(i)
...
h
e
l
l
o

- 14,854
- 11
- 100
- 103

- 25,705
- 7
- 65
- 65
Try the __length_hint__() method from any iterator object:
iter(...).__length_hint__() > 0

- 451
- 3
- 7
You can tee
the iterator using, itertools.tee
, and check for StopIteration
on the teed iterator.

- 96,888
- 11
- 64
- 71
hasNext
somewhat translates to the StopIteration
exception, e.g.:
>>> it = iter("hello")
>>> it.next()
'h'
>>> it.next()
'e'
>>> it.next()
'l'
>>> it.next()
'l'
>>> it.next()
'o'
>>> it.next()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
StopIteration
docs: http://docs.python.org/library/exceptions.html#exceptions.StopIteration- Some article about iterators and generator in python: http://www.ibm.com/developerworks/library/l-pycon.html

- 181,842
- 47
- 306
- 310
No. The most similar concept is most likely a StopIteration exception.

- 46,512
- 18
- 65
- 82
-
13
-
5Right: exceptions should be used to handle errors, not to define the normal flow of control. – Giorgio Dec 24 '12 at 20:23
I believe python just has next() and according to the doc, it throws an exception is there are no more elements.

- 3,734
- 3
- 31
- 49
The use case that lead me to search for this is the following
def setfrom(self,f):
"""Set from iterable f"""
fi = iter(f)
for i in range(self.n):
try:
x = next(fi)
except StopIteration:
fi = iter(f)
x = next(fi)
self.a[i] = x
where hasnext() is available, one could do
def setfrom(self,f):
"""Set from iterable f"""
fi = iter(f)
for i in range(self.n):
if not hasnext(fi):
fi = iter(f) # restart
self.a[i] = next(fi)
which to me is cleaner. Obviously you can work around issues by defining utility classes, but what then happens is you have a proliferation of twenty-odd different almost-equivalent workarounds each with their quirks, and if you wish to reuse code that uses different workarounds, you have to either have multiple near-equivalent in your single application, or go around picking through and rewriting code to use the same approach. The 'do it once and do it well' maxim fails badly.
Furthermore, the iterator itself needs to have an internal 'hasnext' check to run to see if it needs to raise an exception. This internal check is then hidden so that it needs to be tested by trying to get an item, catching the exception and running the handler if thrown. This is unnecessary hiding IMO.

- 1,072
- 8
- 9
-
1For this use case, you can use [itertools.cycle](https://docs.python.org/3/library/itertools.html?highlight=itertools#itertools.cycle) – eaglebrain Aug 22 '18 at 13:15
Maybe it's just me, but while I like https://stackoverflow.com/users/95810/alex-martelli 's answer, I find this a bit easier to read:
from collections.abc import Iterator # since python 3.3 Iterator is here
class MyIterator(Iterator): # need to subclass Iterator rather than object
def __init__(self, it):
self._iter = iter(it)
self._sentinel = object()
self._next = next(self._iter, self._sentinel)
def __iter__(self):
return self
def __next__(self): # __next__ vs next in python 2
if not self.has_next():
next(self._iter) # raises StopIteration
val = self._next
self._next = next(self._iter, self._sentinel)
return val
def has_next(self):
return self._next is not self._sentinel

- 2,688
- 1
- 20
- 30
No, there is no such method. The end of iteration is indicated by a StopIteration
(more on that here).
This follows the python principle EAFP (easier to ask for forgiveness than permission). A has_next
method would follow the principle of LBYL (look before you leap) and contradicts this core python principle.
This interesting article explains the two concepts in more detail.

- 5,141
- 5
- 38
- 59
It is also possible to implement a helper generator that wraps any iterator and answers question if it has next value:
def has_next(it):
first = True
for e in it:
if not first:
yield True, prev
else:
first = False
prev = e
if not first:
yield False, prev
for has_next_, e in has_next(range(4)):
print(has_next_, e)
Which outputs:
True 0
True 1
True 2
False 3
The main and probably only drawback of this method is that it reads ahead one more element, for most of tasks it is totally alright, but for some tasks it may be disallowed, especially if user of has_next()
is not aware of this read-ahead logic and may missuse it.
Code above works for infinite iterators too.
Actually for all cases that I ever programmed such kind of has_next()
was totally enough and didn't cause any problems and in fact was very helpful. You just have to be aware of its read-ahead logic.

- 14,883
- 6
- 36
- 69
The way has solved it based on handling the "StopIteration" execption is pretty straightforward in order to read all iterations :
end_cursor = False
while not end_cursor:
try:
print(cursor.next())
except StopIteration:
print('end loop')
end_cursor = True
except:
print('other exceptions to manage')
end_cursor = True

- 1
- 1
I think there are valid use cases for when you may want some sort of has_next
functionality, in which case you should decorate an iterator with a has_next
defined.
Combining concepts from the answers to this question here is my implementation of that which feels like a nice concise solution to me (python 3.9):
_EMPTY_BUF = object()
class BufferedIterator(Iterator[_T]):
def __init__(self, real_it: Iterator[_T]):
self._real_it = real_it
self._buf = next(self._real_it, _EMPTY_BUF)
def has_next(self):
return self._buf is not _EMPTY_BUF
def __next__(self) -> _T_co:
v = self._buf
self._buf = next(self._real_it, _EMPTY_BUF)
if v is _EMPTY_BUF:
raise StopIteration()
return v
The main difference is that has_next
is just a boolean expression, and also handles iterators with None
values.
Added this to a gist here with tests and example usage.

- 899
- 7
- 15
There is no has_next
in Python, but you can achieve has_next
functionality for iterators like this:
class BSTIterator:
def __init__(self, root: Optional[TreeNode]):
def inorder_dfs(root):
if root is None:
return
yield from inorder_dfs(root.left)
yield root.val
yield from inorder_dfs(root.right)
self.inorder_generator = inorder_dfs(root)
self.has_next = None
def next(self) -> int:
if self.has_next is not None:
temp = self.has_next
self.has_next = None
return temp
return next(self.inorder_generator)
def hasNext(self) -> bool:
if self.has_next is not None:
return True
try:
self.has_next = next(self.inorder_generator)
return True
except StopIteration:
return False

- 127,765
- 105
- 273
- 257

- 1
- 1
The way I solved my problem is to keep the count of the number of objects iterated over, so far. I wanted to iterate over a set using calls to an instance method. Since I knew the length of the set, and the number of items counted so far, I effectively had an hasNext
method.
A simple version of my code:
class Iterator:
# s is a string, say
def __init__(self, s):
self.s = set(list(s))
self.done = False
self.iter = iter(s)
self.charCount = 0
def next(self):
if self.done:
return None
self.char = next(self.iter)
self.charCount += 1
self.done = (self.charCount < len(self.s))
return self.char
def hasMore(self):
return not self.done
Of course, the example is a toy one, but you get the idea. This won't work in cases where there is no way to get the length of the iterable, like a generator etc.

- 836
- 12
- 12
very interesting question, but this "hasnext" design had been put into leetcode: https://leetcode.com/problems/iterator-for-combination/
here is my implementation:
class CombinationIterator:
def __init__(self, characters: str, combinationLength: int):
from itertools import combinations
from collections import deque
self.iter = combinations(characters, combinationLength)
self.res = deque()
def next(self) -> str:
if len(self.res) == 0:
return ''.join(next(self.iter))
else:
return ''.join(self.res.pop())
def hasNext(self) -> bool:
try:
self.res.insert(0, next(self.iter))
return True
except:
return len(self.res) > 0

- 525
- 5
- 8