Change language

Python | Find the smallest element greater than K

|

Let’s look at all the approaches to solving this problem, from naive to one-liners, so that they can be used in programming as needed.

Method # 1: The Naive Method
Using a loop, we continue to reinitialize the named variable if we find an element less than the previous value than the named variable and greater than K.

 ` # Python3 demo code ` ` # the smallest number is greater than K ` ` # using the naive method `   ` # Initializing list ` ` test_list ` ` = ` ` [` ` 1 ` `, ` ` 4 ` `, ` ` 7 ` `, ` ` 5 ` `, ` ` 10 ` `] `   ` # Initialize to ` ` k ` ` = ` ` 6 `   ` # Print original list ` ` print ` ` (` `" The original list is: "` ` + ` ` str ` ` (test_list )) `   ` # Using the naive method ` ` # find the smallest number ` ` # more K ` ` min_val ` ` = ` ` 10000000 ` ` for ` ` i ` ` in ` ` test_list: ` ` if ` ` min_val" i ` ` and ` ` i" k: ` ` min_val ` ` = ` ` i `   ` # Print 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 linking it to a generator expression can accomplish this task much more succinctly and therefore more useful when you need to save time.

< / p>
 ` # Python3 demo code ` ` # the smallest number is greater than K ` ` # using min () + generator expression `   ` # Initializing list ` ` test_list ` ` = ` ` [` ` 1 ` `, ` ` 4 ` `, ` ` 7 ` `, ` ` 5 ` `, ` ` 10 ` `] `   ` # Initialize to ` ` k ` ` = ` ` 6 `    ` # Print original list ` ` print ` ` (` ` "The original list is:" ` ` + ` ` str ` ` (test_list)) `   ` # Using min () + generator expression ` ` # find the smallest number ` ` # more K ` ` min_val ` ` = ` ` min ` ` (i ` ` for ` ` i ` ` in ` ` test_list ` ` if ` ` i" k) `   ` # Print 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 () `
A similar approach to the method above, just to filter out numbers in the list greater than k, ` filter () ` instead of a generator expression is used in this approach. Works the same as above.

 ` # Python3 demo code ` ` # lowest number more K ` ` # using min () + filter () ` ` `  ` # Initializing list ` ` test_list ` ` = ` ` [` ` 1 ` `, ` ` 4 ` `, ` ` 7 ` `, ` ` 5 ` `, ` ` 10 ` `] `   ` # Initialize to ` ` k ` ` = ` ` 6 `   ` # Print original list ` ` print ` ` (` ` "The original list is:" ` ` + ` ` str ` ` (test_list)) `   ` # Using min () + filter () ` ` # find the smallest number ` ` # more K ` ` min_val ` ` = ` ` min ` ` (` ` filter ` ` (` ` lambda ` ` i: i" k, test_list)) `   ` # Print 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 () ` combined with ` sort () ` performs the task of binary search for us and is therefore a good option to achieve a solution to this problem. ` bisect_right () ` because it returns a strictly larger number, not the number itself, if present in the list.

 ` # Python3 code for demonstration ` ` # the smallest number is greater than K ` ` # using sort () + bisect_right () ` ` from ` ` bisect ` ` import ` ` bisect_right `   ` # Initializing list ` ` test_list ` ` = ` ` [` ` 1 ` `, ` ` 4 ` `, ` ` 7 ` `, ` ` 5 ` `, ` ` 10 ` `] `   ` # Initialize to ` ` k ` ` = ` ` 6 `   ` # Print original list ` ` print ` ` (` `" The original list is: "` ` + ` ` str ` ` (test_list )) `   ` # Using sort () + bisect_right () ` ` # find the smallest number ` ` # more K ` ` test_list.sort () ` ` min_val ` ` = ` ` test_l ist [bisect_right (test_list, k)] `   ` # Print 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 `

Shop

Learn programming in R: courses

\$

Best Python online courses for 2022

\$

Best laptop for Fortnite

\$

Best laptop for Excel

\$

Best laptop for Solidworks

\$

Best laptop for Roblox

\$

Best computer for crypto mining

\$

Best laptop for Sims 4

\$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

NUMPYNUMPY

How to convert Nonetype to int or string?

NUMPYNUMPY

How to specify multiple return types using type-hints

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method