3 ms·
I don’t think you can do that with a generator expression. You would have to write: sum(1 for age in ages if age > 17)
by vore 3y ago
I don’t think you can do that with a generator expression. You would have to write:
sum(1 for age in ages if age > 17)
- Nihilartikel 3y agoIt would eat ram at scale but you could wrap the gen expression with [] for a list comprehension and that would work.
- deleted 3y ago[deleted]
- deleted 3y ago[deleted]
- aynyc 3y agoah, yes, of course, forgot the generator.
- nomel 3y agoIf you're going to go that route, I think this makes more sense: count_over_17 = [age > 17 for age in ages].count(True)
- dragonwriter 3y agoFor a very large sequence traversing it to build a list and then traversing the list to do something you could do in one traversal without creating a list may be undesirable.
- nomel 3y agoIn a very large sequence, you would be using numpy. I assumed the purpose of their example was some sort of clarity, over `sum(age > 17 for age in ages)`, rather than performance.