I am on Python 3.6.7.
I just noticed that a for loop over an empty list does not loop even once.
After some thought, that made some sense to me. I.e.
There is no builtin way to directly check for empty iterators. Empty iterators are generally not considered exceptional. However, you can define compact helpers to explicitly check for empty iterators.
Detecting empty iterators up-front is generally not possible. In some cases, one can guess from the source of an iterator whether it is empty -- for example, iterators of sequences and mappings are empty if their parent is boolean false. However, iterators themselves do not have any indication for content and may in fact be "empty" unless iterated over.
The only reliable means is to check whether an iterator provides items while iterating. One can define a wrapper iterator that raises an error if an iterator provides no items at all.
def non_empty(iterable):
"""Helper to ensure that ``iterable`` is not empty during iteration"""
iterator = iter(iterable)
try:
yield next(iterator) # explicitly check first item
except StopIteration:
raise LookupError(f'{iterable} is empty') from None
yield from iterator # forward iteration of later items
Such a helper can be wrapped around both iterables and iterators, and works in for loops, explicit iter iterators and any other iteration scenario.
>>> iterable = []
>>> for element in non_empty(iterable):
... assert isinstance(element, int)
Traceback (most recent call last):
File "", line 1, in
File "", line 6, in non_empty
LookupError: [] is empty