Python | Find smallest element greater than K

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 MethodUsing 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 kk = 6  # Printing original list print ("The original list is : " + str(test_list))  # Using naive method # to find smallest number# greater than Kmin_val = 10000000for 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 expressionmin() 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 kk = 6  # Printing original list print ("The original list is : " + str(test_list))  # Using min() + generator expression# to find smallest number# greater than Kmin_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 kk = 6  # Printing original list print ("The original list is : " + str(test_list))  # Using min() + filter()# to find smallest number# greater than Kmin_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 kk = 6  # Printing original list print ("The original list is : " + str(test_list))  # Using sort() + bisect_right()# to find smallest number# greater than Ktest_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

Python list-programs
python-list
Python
python-list
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.