The easiest way is probably just sum(1 for _ in gen)
where gen is your generator.
More Related Contents:
- What’s the shortest way to count the number of items in a generator/iterator?
- How can I count the number of items in an arbitrary iterable (such as a generator)?
- Understanding generators in Python
- Lazy Method for Reading Big File in Python?
- Difference between Python’s Generators and Iterators
- What’s the purpose of “send” function on Python generators?
- How do I know if a generator is empty from the start?
- Reading from a frequently updated file
- Resetting generator object in Python
- How to print a generator expression?
- How do I build a numpy array from a generator?
- Can iterators be reset in Python?
- Converting “yield from” statement to Python 2.7 code
- If range() is a generator in Python 3.3, why can I not call next() on a range?
- How to len(generator()) [duplicate]
- Python generator that groups another iterable into groups of N
- Get the nth item of a generator in Python
- How to get the n next values of a generator in a list (python)
- Why does next raise a ‘StopIteration’, but ‘for’ do a normal return?
- Can generators be recursive?
- How to make a custom object iterable?
- Turn functions with a callback into Python generators?
- Best way to receive the ‘return’ value from a python generator
- Handle an exception thrown in a generator
- Handle generator exceptions in its consumer
- How to slice a generator object or iterator?
- What does the “yield” keyword do in Python?
- Why do list comprehensions write to the loop variable, but generators don’t? [duplicate]
- Are Generators Threadsafe?
- Python: powerset of a given set with generators [duplicate]