Let's say I have a list like:
my_list = range(10)
And I want to count how many even numbers there are in the list. Note that I am not interested with the values, I just want the count of them. So I can:
len( [0 for i in my_list if i % 2 == 0] ) # Method 1
len( [i for i in my_list if i % 2 == 0] ) # Method 2
len( [_ for i in my_list if i % 2 == 0] ) # Method 3
Is any of the above methods better than others from the speed or memory perspectives?
Actually I don't even need to construct the list, but I don't want to:
counter = 0
for item in my_list:
if item % 2 == 0:
counter += 1
So, which one is a good way of counting with generators?
PS: The list in my case has more memory-heavy items, that is why I want to optimize if possible.