python: get number of items from list(sequence) with certain condition python: get number of items from list(sequence) with certain condition python python

python: get number of items from list(sequence) with certain condition


You can use a generator expression:

>>> l = [1, 3, 7, 2, 6, 8, 10]>>> sum(1 for i in l if i % 4 == 3)2

or even

>>> sum(i % 4 == 3 for i in l)2

which uses the fact that int(True) == 1.

Alternatively, you could use itertools.imap (python 2) or simply map (python 3):

>>> def my_condition(x):...     return x % 4 == 3... >>> sum(map(my_condition, l))2


You want a generator comprehension rather than a list here.

For example,

l = [1, 4, 6, 7, 30, 2]def my_condition(x):    return x > 5 and x < 20print sum(1 for x in l if my_condition(x))# -> 2print sum(1 for x in range(1000000) if my_condition(x))# -> 14

Or use itertools.imap (though I think the explicit list and generator expressions look somewhat more Pythonic).

Note that, though it's not obvious from the sum example, you can compose generator comprehensions nicely. For example,

inputs = xrange(1000000)      # In Python 3 and above, use range instead of xrangeodds = (x for x in inputs if x % 2)  # Pick odd numberssq_inc = (x**2 + 1 for x in odds)    # Square and add oneprint sum(x/2 for x in sq_inc)       # Actually evaluate each one# -> 83333333333500000

The cool thing about this technique is that you can specify conceptually separate steps in code without forcing evaluation and storage in memory until the final result is evaluated.


This can also be done using reduce if you prefer functional programming

reduce(lambda count, i: count + my_condition(i), l, 0)

This way you only do 1 pass and no intermediate list is generated.