Reputation: 19815
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.
Upvotes: 0
Views: 168
Reputation: 1122172
Use none of the above. Use sum()
and a generator expression:
sum(i % 2 == 0 for i in mylist)
In Python, the bool
boolean type is a subclass of int
and True
has an integer value of 1
, False
has 0
, so you can sum a series of True
and False
results.
The sum()
-with-generator expression only has to keep one boolean in memory at a time, no intermediary list has to be produced and kept around just to calculate a length.
Alternatively, stick to filtering and sum 1
literals:
sum(1 for i in mylist if i % 2 == 0)
This results in fewer objects needing to be added up still.
Upvotes: 8