

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 < x, then return true, otherwise false
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: 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 print(solve(nums, x))
Input
[1,5,8,3,6,9,12,77,55,36,2,5,6,12,87], 50
Output
[1, 5, 8, 3, 6, 9, 12, 36, 2, 5, 6, 12]
- Related Questions & Answers
- Program to find X for special array with X elements greater than or equal X in Python
- Find the element before which all the elements are smaller than it, and after which all are greater in Python
- Count smaller values whose XOR with x is greater than x in C++
- Delete all the nodes from the list that are greater than x in C++
- Replace all values in an R data frame if they are greater than a certain value.
- Find the Number of segments where all elements are greater than X using C++
- Count elements such that there are exactly X elements with values greater than or equal to X in C++
- MongoDB query where all array items are greater than a specified condition?
- C# program to check if all the values in a list that are greater than a given value
- Python program to check if all the values in a list that are greater than a given value
- Python – Filter Tuples Product greater than K
- Program to check if all the values in a list that are greater than a given value in Python
- C++ program to rearrange all elements of array which are multiples of x in increasing order
- How to find numbers in an array that are greater than, less than, or equal to a value in java?
- Program to find number not greater than n where all digits are non-decreasing in python
Advertisements