72

I want to count how many members of an iterable meet a given condition. I'd like to do it in a way that is clear and simple and preferably reasonably optimal.

My current best ideas are:

sum(meets_condition(x) for x in my_list)

and

len([x for x in my_list if meets_condition(x)])

The first one being iterator based is presumably faster for big lists. And it's the same form as you'd use for testing any and all. However it depends on the fact that int(True) == 1, which is somewhat ugly.

The second one seems easier to read to me, but it is different from the any and all forms.

Does anyone have any better suggestions? is there a library function somewhere that I am missing?

Gordon Wrigley
  • 11,015
  • 10
  • 48
  • 62

4 Answers4

89

The iterator based approach is just fine. There are some slight modifications that can emphasize the fact that you are counting:

sum(1 if meets_condition(x) else 0 for x in my_list)
# or 
sum(1 for x in my_list if meets_condition(x))

And as always, if the intent isn't apparent from the code, encapsulate it in descriptively named function:

def count_matching(condition, seq):
    """Returns the amount of items in seq that return true from condition"""
    return sum(1 for item in seq if condition(item))

count_matching(meets_condition, my_list)
Ants Aasma
  • 53,288
  • 15
  • 90
  • 97
  • 3
    True == 1 by definition in Python, so in many cases you can write `sum(...condition... for x in FOO)` or `sum(map(some_predicate, FOO))`. – Ian May 08 '19 at 14:11
18

The first one

sum(meets_condition(x) for x in my_list)

looks perfectly readable and pythonic to me.

If you prefer the second approach I'd go for

len(filter(meets_condition, my_list))

Yet another way could be:

map(meets_condition, my_list).count(True)
ChristopheD
  • 112,638
  • 29
  • 165
  • 179
  • 1
    map and filter seem to be frowned on these days in favor of list comprehensions and generators – Gordon Wrigley Apr 15 '10 at 09:20
  • @tolomea: true (it's known that Guido doesn't like them that much: see http://www.artima.com/weblogs/viewpost.jsp?thread=98196) although currently only `reduce` is scheduled for removal. That being said, as in my answer I think `sum(meets_condition(x) for x in my_list)` is the best solution in this specific case. – ChristopheD Apr 15 '10 at 09:29
  • 1
    reduce isn't really 'scheduled for removal', it's just been moved to the functools module in 3.x. – Thomas Wouters Apr 15 '10 at 09:52
  • @Thomas Wouters: true, good point. removal from the builtins would have been better wording. – ChristopheD Apr 15 '10 at 11:11
  • For the last one, I get the error: `'map' object has no attribute 'count'`. Think you need to wrap in a `list` first – KyleMit Dec 17 '22 at 22:18
5

countif for a list

#counting if a number or string is in a list
my_list=[1,2,3,2,3,1,1,1,1,1, "dave" , "dave"]
one=sum(1 for item in my_list if item==(1))
two=sum(1 for item in my_list if item==(2))
three=sum(1 for item in my_list if item==(3))
dave=sum(1 for item in my_list if item==("dave"))
print("number of one's in my_list > " , one)
print("number of two's in my_list > " , two)
print("number of three's in my_list > " , three)
print("number of dave's in my_list > " , dave)
user3234931
  • 83
  • 1
  • 3
0

I know the question above is very well taken care of already, but if you are new in the python world and happen to be here because you searched for the simple keyword "Countif python" (and google suggested this page as the most relevant page on its list), the following method is very simple to code and run.

You can simply use a boolean value and as Gordon said, it depends on the fact that True==1;

First, create a boolean series using the column's condition and then add up the values.

series = boolian condition
sum(series)

Example: We'd like to know how many students got (GPA = A) and how many received a mark over 80 in Math.

df = pd.DataFrame({'ID': [1,2,3,4,5],
               'Math':[87,83,95,75,60],
               'GPA': ['A','B','A','B','C']})

Answer:

GPA = df.GPA == 'A'
sum(GPA)
Out: 2

Math_Mark = df.Math > 80
sum(Math_Mark)
Out: 3

P.s: I added this answer because google lists this page as the most relevant page when you search for "countif python". No need to give negative, if most of you think this answer shouldn't be here, I'll remove it.

Mede.B
  • 11
  • 1