In this example we will discuss 4 different ways to count number of elements in a list that satisfy certain conditions.

Suppose we have a list of numbers i.e.

We want to count number of elements in the list that satisfy our given conditions. These conditions can be like,

• Number should be Odd i.e. x % 2 == 1
• Number should be Even i.e. x % 2 == 0
• Number should be greater than five i.e. x > 5
• Number should be greater than five and less than twenty i.e. x > 5 and x < 20

There are different ways to do it. Let’s discuss them one by one,

## Use map() & sum() to count elements in a list that satisfy certain conditions

map() function

map() Function in python accepts a function and an iterable like list. Then applies the given function to each element in the list and appends the result of function in a new list. In the end map() returns this new list i.e. list of results.

sum() Function

sum() function adds the items in the given iterable.

Let’s use both of these functions to count elements in a list by condition.

### Count odd numbers in the list

Single line solution is,

Output:

How did it worked ?

Let’s break the above solution in small steps to understand how it worked.

In this case our condition is that number should be odd. So, lets create a lambda function that checks this condition i.e.

Now pass our condition / lambda function to map() and get a map object of results i.e.

The given lambda function is applied to each element in the list and result is stored in the map object. As our lambda function returns a a bool, so map object contains bool elements. We can check the result by printing map object contents i.e.

Output:

Number of True in this bool list represents the count of odd elements in the list. Let’s count the occurrences of True in this map object using sum() function i.e.

This is how we found count of elements in list that satisfies a given condition.

Let’s see some more examples to count elements in list based on conditions using sum() & map()

Output:

Output:

## Use sum() & Generator expression to count elements in list based on conditions

Let’s create a function that uses sum() & Generator Expression to count number of elements in a list that satisfies our given condition i.e.

This function accepts a list and a callback function as argument. Callback function will be our function that checks the condition. This function calls the given callback function on each element in the list and returns a count of elements in list that returns True when passed to our condition i.e. callback.

Let’s use this function to count number of elements in list that satisfies our condition,

Output:

### Count numbers in a list which are greater than 5 but less than 20:

Output:

If no callback function is passed then this function will return the total number of elements in the list i.e.

Output

## Use len() & List comprehension to count elements in list based on conditions

We can use list comprehension to create a new list of elements that satisfies our given condition and then get the length of this new list to find out number of elements in original list that satisfies our condition i.e.

Output:

## Use reduce() function to count elements in list based on conditions

#### Count of numbers in a list which are greater than 5

Output:

reduce() function will iterate over the list check condition & add each True values to count number of elements that satisfies the given condition.

Complete example is as follows,

Output:

• ### Best books to learn C++11

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.