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)) ` |

*chevron_right*

*filter_none*

**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)) ` |

*chevron_right*

*filter_none*

**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)) ` |

*chevron_right*

*filter_none*

**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)) ` |

*chevron_right*

*filter_none*

**Output :**

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

## Recommended Posts:

- Python - Nth smallest Greater than K
- Python - Find the frequency of numbers greater than each element in a list
- Python | Get the Index of first element greater than K
- Python - Test if greater than preceding element in Tuple List
- Python - Test if common values are greater than K
- Python | Check if all the values in a list that are greater than a given value
- Python | Number of values greater than K in list
- Python | Remove tuples from list of tuples if greater than n
- Python | Indices of numbers greater than K
- Python | Select dictionary with condition given key greater than k
- Python | Records with Key's value greater than K
- Python - Storing Elements Greater than K as Dictionary
- Python - Filter and Double keys greater than K
- Python - Rearrange elements second index greater than first
- Python - Extract Dictionary Items with Summation Greater than K
- Python - Remove keys with Values Greater than K ( Including mixed values )
- Python - Extract elements with Frequency greater than K
- Python - Replace Elements greater than K
- Python - Extract dictionaries with values sum greater than K
- Python - Filter Tuples Product greater than K

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.