In this post, we are going to learn about how count number of elements in Python list by condition. There are multiple ways to count a number of elements in a python list, so we will learn each of these methods one by one with the help of examples. so let us begin our tutorial. We can these ways with string and number list.
1. Count number elements by condition in list using loop
In this code snippet, we are using the for-loop to python list count elements with conditions or criteria. We are iterating over every element of a list and checking the condition if it true then increment by 1. This is a simple matching and counting process to get the counts.
#element count of a list that passes condition
list_numbers = [78,99,66,44,50,30,45,15,25,20]
count = 0
for item in list_numbers:
if item%5==0:
count += 1
print("count of list elements that pass given condition:",count)
Output
count of list elements that pass condition: 6
2. Using list comprehension
Here, in the code snippet, we are using the list comprehension to count the numbers of list element that passes the given condition(list element which are divided by 5). It would be easy to understand with an example so let us jump to example.
#element count of list that pass condition
list_numbers = [78,99,66,44,50,30,45,15,25,20]
element_count = len([item for item in list_numbers if item%5== 0])
print("count of list elements that pass given condition:",element_count)
Output
count of list elements that pass given condition: 6
3. Count Number of Non-zero elements in List
In this example, we are will count the number of elements that are non-zero. To find a number of non-zero elements in the list, we can simply change the condition if item ==0.
list_numbers = [78,99,66,44,50,30,45,0,0,0]
element_count = len([item for item in list_numbers if item!= 0])
print("Total count of list elements that pass given condition:",element_count)
Output
Total count of list elements that pass given condition: 7
4. sum() and generator expression To count In list
In this code example, we are using sum() function with a generator expression. The condition runs on each element of the list and returns true for list element that passes the condition, by using the sum() method we are counting the number of elements in the list returning true.
#Program to count number of list elements pass condition
list_numbers = [78,99,66,44,50,30,45,15,25,20]
count = 0
count = sum(True for i in list_numbers if i % 5 == 0)
print("count of list elements that pass given condition:",count)
Output
count of list elements that pass given condition: 6
5. sum() and map() to find element count with condition or criteria
The map() function takes two argument map(fun,iterable),the iterable (string,tuple,list,dictionary) and the function is applied on every element of iterable and return an map object(iterable).
Since we are using the function inside another function then this is the best use case to apply the lambda function. So the map() function will take the first argument in the form of a lambda function.
Here sum() is using with map() function to get the count of all elements of the list that is divisible by 5.
Let understand with example, the lambda function is to check the condition of divisible by 5.
#Program to count number of list elements pass a given condition
list_numbers = [78,99,66,44,50,30,45,15,25,20]
count = 0
count = sum(map(lambda item: item % 5 == 0, list_numbers))
print("count of list elements that pass condition:",count)
Output
count of list elements that pass condition: 6
6. reduce() with lambda count list element with condition or criteria
Lambada is an anonymous(without name) function that has many parameters but the body of the function can only contain one expression.
The best use cases of lambda functions is with another function arguments or when we need a single line logic that can be accommodated in short code.
In this example we are going to use the sum() , map() and reduce() functions to count the items in a list that are divisible by 5.
so let us check the below code to understand this.
from functools import reduce
#Program to count number of list elements pass condition
list_numbers = [78,99,66,44,50,30,45,15,25,20]
#count = 0
#count = sum(map(lambda item: item % 5 == 0, list_numbers))#
result_count = reduce(lambda count, item: count + (item % 5 == 0), list_numbers, 0)
print("count of list elements that pass condition:",result_count)
Output
count of list elements that pass condition: 6
Conclusion:
We hope you learnt the different approaches to Count number of elements in Python list by condition to filter the data.
Happy Learning!!