Skip to content
Related Articles

Related Articles

Python | Find smallest element greater than K
  • Last Updated : 26 Nov, 2018

Given a list, write a Python program to find the smallest number which is greater than a specific element K.

Let’s see all the approaches to solve this problem, from naive to one-liners so that they can be used in programming whenever required.

Method #1 : Naive Method
Using loop we keep on re-initializing the named variable if we find the element smaller than the previous value than the named variable and greater than K.




# Python3 code to demonstrate 
# smallest number greater than K
# using naive method 
  
# Initializing list 
test_list = [1, 4, 7, 5, 10]
  
# Initializing k
k = 6
  
# Printing original list 
print ("The original list is : " + str(test_list))
  
# Using naive method 
# to find smallest number
# greater than K
min_val = 10000000
for i in test_list :
    if min_val > i and i > k :
        min_val = i
  
# Printing result 
print ("The minimum value greater than 6 is : " + str(min_val))

Output :

The original list is : [1, 4, 7, 5, 10]
The minimum value greater than 6 is : 7

 



Method #2 : Using min() + generator expression
min() returns the minimum number in a sequence and coupling it with a generator expression can perform this task in much concise way and hence more useful when required to save time.




# Python3 code to demonstrate 
# smallest number greater than K
# using min() + generator expression
  
# Initializing list 
test_list = [1, 4, 7, 5, 10]
  
# Initializing k
k = 6
  
# Printing original list 
print ("The original list is : " + str(test_list))
  
# Using min() + generator expression
# to find smallest number
# greater than K
min_val = min(i for i in test_list if i > k)
  
# Printing result 
print ("The minimum value greater than 6 is : " + str(min_val))

Output :

The original list is : [1, 4, 7, 5, 10]
The minimum value greater than 6 is : 7

 

Method #3 : min() + filter()
Similar approach to method above, just to filter the numbers in list greater than k, filter() instead of generator expression is used in this approach. Works in a similar way as above.




# Python3 code to demonstrate 
# smallest number greater than K
# using min() + filter()
  
# Initializing list 
test_list = [1, 4, 7, 5, 10]
  
# Initializing k
k = 6
  
# Printing original list 
print ("The original list is : " + str(test_list))
  
# Using min() + filter()
# to find smallest number
# greater than K
min_val = min(filter(lambda i: i > k, test_list))
  
# Printing result 
print ("The minimum value greater than 6 is : " + str(min_val))

Output :

The original list is : [1, 4, 7, 5, 10]
The minimum value greater than 6 is : 7

 
Method #4 : Using sort() + bisect_right()
bisect_right() coupled with sort() performs the task of binary search for us, and hence is a good option to achieve the solution to this problem. bisect_right() because it returns strictly greater number, not the number itself if it is present in the list.




# Python3 code to demonstrate 
# smallest number greater than K
# using sort() + bisect_right()
from bisect import bisect_right
  
# Initializing list 
test_list = [1, 4, 7, 5, 10]
  
# Initializing k
k = 6
  
# Printing original list 
print ("The original list is : " + str(test_list))
  
# Using sort() + bisect_right()
# to find smallest number
# greater than K
test_list.sort()
min_val = test_list[bisect_right(test_list, k)]
  
# Printing result 
print ("The minimum value greater than 6 is : " + str(min_val))

Output :

The original list is : [1, 4, 7, 5, 10]
The minimum value greater than 6 is : 7

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :