Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to filter all values which are greater than x in an array
Filtering arrays based on a condition is a common task in Python programming. The filter() function provides an efficient way to extract elements that meet specific criteria. In this example, we'll filter numbers that are less than a given threshold value.
Problem Statement
Given a list of numbers and a threshold value x, we need to find all numbers that are less than x using Python's filter() function.
For example, if nums = [1,5,8,3,6,9,12,77,55,36,2,5,6,12,87] and x = 50, the output should be [1, 5, 8, 3, 6, 9, 12, 36, 2, 5, 6, 12].
Solution Approach
To solve this problem, we will follow these steps −
Define a function that takes the array and threshold as arguments
Use
filter()with a lambda function to check if each element is less than xConvert the filter object to a list and return the result
Using filter() with Lambda Function
The filter() function takes a function and an iterable, returning elements where the function returns True ?
def solve(nums, x):
left_items = filter(lambda a: a < x, nums)
return list(left_items)
nums = [1,5,8,3,6,9,12,77,55,36,2,5,6,12,87]
x = 50
result = solve(nums, x)
print("Original array:", nums)
print("Threshold value:", x)
print("Filtered result:", result)
Original array: [1, 5, 8, 3, 6, 9, 12, 77, 55, 36, 2, 5, 6, 12, 87] Threshold value: 50 Filtered result: [1, 5, 8, 3, 6, 9, 12, 36, 2, 5, 6, 12]
Alternative Approaches
Using List Comprehension
List comprehension provides a more Pythonic way to filter elements ?
def solve_with_comprehension(nums, x):
return [num for num in nums if num < x]
nums = [1,5,8,3,6,9,12,77,55,36,2,5,6,12,87]
x = 50
result = solve_with_comprehension(nums, x)
print("Using list comprehension:", result)
Using list comprehension: [1, 5, 8, 3, 6, 9, 12, 36, 2, 5, 6, 12]
Using a Regular Function
Instead of lambda, you can define a separate function for more complex filtering logic ?
def is_less_than_threshold(value, threshold):
return value < threshold
def solve_with_function(nums, x):
return list(filter(lambda a: is_less_than_threshold(a, x), nums))
nums = [1,5,8,3,6,9,12,77,55,36,2,5,6,12,87]
x = 50
result = solve_with_function(nums, x)
print("Using separate function:", result)
Using separate function: [1, 5, 8, 3, 6, 9, 12, 36, 2, 5, 6, 12]
Comparison of Methods
| Method | Readability | Performance | Best For |
|---|---|---|---|
filter() with lambda |
Good | Memory efficient | Functional programming style |
| List comprehension | Excellent | Fast | Simple filtering conditions |
| Regular function | Good | Memory efficient | Complex filtering logic |
Conclusion
The filter() function with lambda expressions provides an efficient way to filter arrays based on conditions. For simple filtering, list comprehension is often more readable and Pythonic. Choose the method that best fits your specific use case and coding style.
