if any(True for _ in iterator):
print('iterator had at least one element')
if all(False for _ in iterator):
print('iterator was empty')
Note that this will consume the first element of the iterable if it has at least one element.
More Related Contents:
- How to build a basic iterator?
- How can I iterate over files in a given directory?
- Get the first item from an iterable that matches a condition
- Difference between Python’s Generators and Iterators
- Iterate over all pairs of consecutive items in a list [duplicate]
- Circular list iterator in Python
- zip variable empty after first use
- How to print a generator expression?
- Why does defining __getitem__ on a class make it iterable in python?
- Can iterators be reset in Python?
- What’s the shortest way to count the number of items in a generator/iterator?
- If range() is a generator in Python 3.3, why can I not call next() on a range?
- Is close() necessary when using iterator on a Python file object [duplicate]
- Is there any built-in way to get the length of an iterable in python?
- Is there an expression for an infinite iterator?
- What does “three dots” in Python mean when indexing what looks like a number?
- When is not a good time to use python generators?
- How to convert an iterable to a stream?
- Making a python iterator go backwards?
- python groupby behaviour?
- split a generator/iterable every n items in python (splitEvery)
- Iterate over the lines of a string
- Why does next raise a ‘StopIteration’, but ‘for’ do a normal return?
- Performance Advantages to Iterators?
- Python range() and zip() object type
- How can I iterate twice over a given iterator (reuse the data, “reset” the iterator etc.)? Why does iterating twice give a blank result?
- Why can’t I iterate twice over the same iterator? How can I “reset” the iterator or reuse the data?
- How do I re-map python dict keys
- What does it mean to “consume” in Python? In an iterator?
- hasnext() for Python iterators?