Python - Numbers in a list within a given range
Last Updated :
31 Jan, 2025
We are given a list and we are given a range we need to count how many number lies in the given range. For example, we are having a list n = [5, 15, 25, 35, 45, 55, 65, 75] and we are having range lower=20, upper=60 so we need to count how many element lies between this range so that the output should be 4 in this case.
Using List Comprehension
List comprehension is one of the most efficient ways to solve this problem. It allows us to create a new list by filtering out the numbers that don’t fall within the specified range.
Python
n = [5, 15, 25, 35, 45, 55, 65, 75]
l = 20
u = 60
# Count numbers in range using list comprehension
cnt = len([i for i in n if l <= i <= u])
print(cnt)
Explanation:
- List comprehension iterates through the list n and checks if each element i is within the specified range (l <= i <= u).
- len() function then counts how many elements satisfy the condition, returning the total count of numbers within the range
Other methods that we can use to count how many numbers in a list fall within a specific range are:
Using filter() and len()
filter() function is built into Python and allows us to filter a list based on a condition. It returns only the elements that satisfy the condition. After filtering the numbers that fall within the range we use len() function to count how many numbers are left.
Python
n = [5, 15, 25, 35, 45, 55, 65, 75]
l = 20
u = 60
# Using filter and len
c = len(list(filter(lambda num: l <= n <= u, n)))
print(c)
Explanation:
- filter() function filters elements in the numbers list based on the condition lower <= num <= upper, keeping only the numbers within the specified range.
- len() function counts number of filtered elements and returns the total count of numbers that fall within range.
Using sum() with a Generator Expression
This method uses sum() function which normally adds up numbers.
Python
n = [5, 15, 25, 35, 45, 55, 65, 75]
l = 20
u = 60
# Using sum with generator expression
c = sum(1 for num in n if l <= n <= u)
print(c)
Explanation:
- Generator expression iterates over each number in
n
and checks if it falls within the range [l, u]
. - For each number that meets condition
1
is yielded and sum()
adds these ones to count total numbers in range
Using for Loop
Using a simple for loop is the most straightforward and easy-to-understand method. We loop through each number in the list check if it is within range and increase the count if it is.
Python
n = [5, 15, 25, 35, 45, 55, 65, 75]
l = 20
u = 60
# Simple loop method
c = 0
for num in n:
if l <= num <= u:
c += 1
print(c)
Explanation:
- Loop iterates through each number in the list
n
and checks if it falls within specified range [l, u]
. - For each number that meets condition counter
c
is incremented ultimately giving count of numbers within range
Using numpy
We use numpy to create an array of numbers. We then use a condition to filter out the numbers within the range and numpy.sum() counts how many elements satisfy that condition.
Python
import numpy as np
# Using numpy for large datasets
n = np.array([5, 15, 25, 35, 45, 55, 65, 75])
l = 20
u = 60
# Count numbers within the range using numpy
c = np.sum((n >= l) & (n <= u))
print(c)
Explanation:
- Expression (n >= l) & (n <= u) generates a boolean array indicating which numbers are within the range.
- np.sum() counts the True values, giving the total number of numbers within the range.
Similar Reads
Python - Generate random numbers within a given range and store in a list Our task is to generate random numbers within a given range and store them in a list in Python. For example, you might want to generate 5 random numbers between 20 and 40 and store them in a list, which could look like this: [30, 34, 31, 36, 30]. Let's explore different methods to do this efficientl
3 min read
range() to a list in Python In Python, the range() function is used to generate a sequence of numbers. However, it produces a range object, which is an iterable but not a list. If we need to manipulate or access the numbers as a list, we must explicitly convert the range object into a list. For example, given range(1, 5), we m
2 min read
Python - Make a list of intervals with sequential numbers Creating a list of sequential numbers in Python allows us to generate a range of values within a defined interval. We can customize the sequence by adjusting the starting point, ending point, and step size. This article will explore various methods to list intervals with sequential numbers. Using it
3 min read
Python - Number of values greater than K in list In Python, Filtering data based on specific conditions is usually necessary. Counting the number of values greater than a given threshold K is a common task in data analysis, sorting, and threshold-based filtering in various applications. Python provides multiple methods to perform this efficiently.
2 min read
How to clamp floating numbers in Python Clamping is a method in which we limit a number in a range or in between two given numbers. When we clamped a number then it holds the value between the given range. If our clamped number is lower than the minimum value then it holds the lower value and if our number is higher than the maximum value
2 min read