Number of items in a Python list with conditions or criteria

by Alex
Number of items in a Python list with conditions or criteria

In this post, we’ll look at how to find out how many items in a Python list meet certain conditions or criteria. If you just want to find the number of specific items in the list, use the .count()method
>>> list_numbers = [1, 2, 2, 5, 5, 7, 4, 2, 1]
>>> print(list_numbers.count(2))
3
There are several ways to do this counting, and we’ll explore each of them with examples. So, let’s get started.

1. Using the for loop to count in a Python list

In this code snippet, we use a for loop to count the items in a Python list that meet the conditions or criteria. We loop through each item in the list and check the condition, if it’s true, we increment the counter by 1. It’s a simple matching and counting process to get the number we’re interested in.
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("the number of list elements satisfying the given condition:", count)
the number of list items, that satisfy the given condition: 6

2. Using len() with list comprehension to count in a Python list

In the code snippet below, we use list comprehension to create a new list whose elements meet a given condition, after which we get the length of the compiled list. This is much easier to understand with an example, so let’s skip to it.
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(
“the number of list elements satisfying the given condition:”,
element_count
)
the number of list items that satisfy the given condition: 6

Counting non-zero elements

In this example, we find the total number of non-zero elements. To find the number of non-zero list members, we can simply change the condition to 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(
"the number of list elements satisfying the given condition:",
element_count
)
the number of list items that satisfy the given condition: 7

3. sum() and a generator expression for counting in a Python list

In this code example, we use sum() with a generator expression. Each item in the list is tested by the condition and for those items that satisfy it, we return True. The sum() method in turn counts the total number of true values.
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(
"the number of list elements that satisfy the given condition:",
count
)
the number of list items that satisfy the given condition: 6

4. sum() and map() for counting Python list items with conditions or criteria

The map(fun, iterable) function takes two arguments: an iterable object (which can be a string, tuple, list, or dictionary) and a function that applies to each of its elements – and returns a map object (iterator). To apply one function inside another, a lambda function is ideal. Thus map() will take the first argument as a lambda function. Here sum() is used with map() to get the number of all list items that are divisible by 5. Let’s look at an example where the passed lambda function is intended to filter list members not divisible by 5.
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(
"the number of list items that satisfy the given condition:",
count
)
the number of list items that satisfy the given condition: 6

5. reduce() with a lambda function to count the elements of a Python list with a condition or criteria

A lambda is an anonymous (unnamed) function that can take many parameters, but the body of the function must contain only one expression. Lambda functions are most often used to pass as arguments to other functions or to write more concise code. In this example, we are going to use sum(), map(), and reduce() to count items in a list that are divisible by 5. The code below clearly demonstrates this.
from functools import reduce
list_numbers = [78, 99, 66, 44, 50, 30, 45, 15, 25, 20]
result_count = reduce(
lambda count, item: count + (item % 5 == 0),
list_numbers,
)
print(
“the number of list elements satisfying the specified condition:”,
result_count
)
the number of list items that satisfy the given condition: 6 I hope that you have learned about the different approaches to counting items in a Python list using a condition or criteria to filter the data. Have fun learning!

Related Posts

LEAVE A COMMENT