Calls to itertools.imap()
in Python 2 or map()
in Python 3 can be replaced by equivalent generator expressions:
sum(1 for dummy in it)
This also uses a lazy generator, so it avoids materializing a full list of all iterator elements in memory.
More Related Contents:
- How can I count the number of items in an arbitrary iterable (such as a generator)?
- What exactly are iterator, iterable, and iteration?
- Difference between Python’s Generators and Iterators
- How to print a generator expression?
- What are iterator, iterable, and iteration?
- Can iterators be reset in Python?
- If range() is a generator in Python 3.3, why can I not call next() on a range?
- Is there an expression for an infinite iterator?
- When is not a good time to use python generators?
- Length of generator output [duplicate]
- Why does next raise a ‘StopIteration’, but ‘for’ do a normal return?
- How to implement an efficient infinite generator of prime numbers in Python?
- Iterate over all pairs of consecutive items in a list [duplicate]
- What can you use generator functions for?
- Circular list iterator in Python
- Getting number of elements in an iterator in Python
- How to read file N lines at a time?
- Is there a generator version of `string.split()` in Python?
- Python random sample with a generator / iterable / iterator
- How to look ahead one element (peek) in a Python generator?
- What does “three dots” in Python mean when indexing what looks like a number?
- Cartesian product of a dictionary of lists
- python groupby behaviour?
- How to check if an object is a generator object in python?
- split a generator/iterable every n items in python (splitEvery)
- Iterate over the lines of a string
- python 3 print generator
- Why can’t I iterate twice over the same iterator? How can I “reset” the iterator or reuse the data?
- Why do I get “TypeError: ‘int’ object is not iterable” when trying to sum digits of a number? [duplicate]
- How to use fit_generator with multiple inputs