- Related Questions & Answers
- Find Smallest Letter Greater Than Target in Python
- Python - Get the Index of first element greater than K
- How to find the smallest number greater than x in Python?
- Python Indices of numbers greater than K
- Python - Number of values greater than K in list
- Count of subarrays whose maximum element is greater than k in C++
- Python program to find k'th smallest element in a 2D array
- Count subarrays with all elements greater than K in C++
- Find k-th smallest element in BST (Order Statistics in BST) in C++
- Count of alphabets having ASCII value less than and greater than k in C++
- C# program to find K’th smallest element in a 2D array
- Program to find lexicographically smallest subsequence of size k in Python
- JavaScript - Find the smallest n digit number or greater
- Smallest Integer Divisible by K in Python
- Adding elements of an array until every element becomes greater than or equal to k in C++.

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

During data analysis using python we come across many scenarios where we have to filter out elements from a list meeting certain criteria. In this article we will see how to get an element from a list which is greater than the element but smallest among all such elements which are greater than the given element.

We design a for loop to go through each element of the list while meeting the general criteria of it’s value greater than k. Then for all such elements we apply the min function to get the minimum value.

listA = [1,5,6, 7,11,14] # Original list print("Given list : ",listA) k = 8 # using min res = min(i for i in listA if i > k) # Result print("Missing elements from the list : \n" ,res)

Running the above code gives us the following result −

Minimum element gerater than k : 11

Here we use the lambda function to get elements whose value is greater than K. Then apply the filter function to get only those values. Finally apply the min function to get the minimum value form this list.

listA = [1,5,6, 7,11,14] # printing original list print("Given list : ",listA) k = 8 # using min res = min(filter(lambda i: i > k, listA)) # Result print("Minimum element gerater than k : \n" ,res)

Running the above code gives us the following result −

Minimum element gerater than k : 11

The bisect_right function is available in the bisect module. It bisects a list at a point greater than or equal to a certain parameter value supplied to it. In this example we take the list, sort it and then apply the bisect_right function. We get the index of the element which is greater than the required value.

from bisect import bisect_right listA = [1,5,6, 7,11,14] # printing original list print("Given list : ",listA) k = 8 listA.sort() # Using bisect_right res = listA[bisect_right(listA, k)] # Result print("Minimum element gerater than k : \n" ,res)

Running the above code gives us the following result −

Minimum element gerater than k : 11

Advertisements