Assuming that I have a list with a huge number of items,
l = [ 1, 4, 6, 30, 2, ... ]
I want to get the number of items from that list, where an item satisfies a certain condition. My first thought was:
count = len([i for i in l if my_condition(l)])
But if the filtered list also has a great number of items, I think that creating a new list for the filtered result is just a waste of memory. For efficiency, IMHO, the above call can't be better than:
count = 0
for i in l:
if my_condition(l):
count += 1
Is there any functional-style way to get the # of items that satisfy the condition without generating a temporary list?