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
Selected Reading
Program to filter all values which are greater than x in an array
Suppose we have a list of numbers called nums. We also have another number x. We have to find all numbers from nums which are less than x by filtering. In we use python there is one filter() method that takes function as argument and filter using this function.
So, if the input is like nums = [1,5,8,3,6,9,12,77,55,36,2,5,6,12,87] x = 50, then the output will be [1, 5, 8, 3, 6, 9, 12, 36, 2, 5, 6, 12]
To solve this, we will follow these steps −
define a function f, this will take an argument a
if a
left_items := filter nums using the function f
convert filter object left_items to list and return
Example
Let us see the following implementation to get better understanding
def solve(nums, x): left_items = filter(lambda a: aInput
[1,5,8,3,6,9,12,77,55,36,2,5,6,12,87], 50Output
[1, 5, 8, 3, 6, 9, 12, 36, 2, 5, 6, 12]
Advertisements
