Python | Find smallest element greater than K Last Updated : 11 Jul, 2025 Comments Improve Suggest changes 5 Likes Like Report This problem involves searching through a list to identify the smallest number that is still larger than K. We will explore different methods to achieve this in Python In this article, we'll look at simple ways to find the smallest element greater than k in a list using Python.Using Binary SearchBinary search on a sorted array helps find smallest element greater than K by efficiently locating the insertion point. It's particularly useful for handling multiple queries on the same array after sorting.Example: Python from bisect import bisect_right # List 'a' and value 'k' a = [1, 4, 7, 5, 10] k = 6 # Sort the list for binary search a.sort() # bisect_right finds the index where 'k' would fit # returning the index of the first element k res = a[bisect_right(a, k)] # Smallest element greater than 'k' print(str(res)) Output7 Explanation:Code starts by importing the bisect_right function from the bisect module, which helps find the insertion point for a given value in a sorted list.It defines a list a and a target value k.list a is sorted to ensure that the binary search can be applied correctly.bisect_right(a, k) is used to find the index where k would fit in the sorted list a. This index points to the first element greater than k.Table of ContentUsing sorted Set with Binary SearchUsing Linear SearchUsing sorted Set with Binary SearchUsing sorted Set allows maintaining a sorted collection while enabling efficient insertion and querying. This method uses a binary search to quickly find the smallest element greater than K in dynamic datasets.Example: Python from sortedcontainers import SortedSet # List 'a' and value 'k' a = [1, 4, 7, 5, 10] k = 6 # Create SortedSet to keep elements sorted s = SortedSet(a) # bisect_right finds the index of the smallest element greater than 'k' res = s.bisect_right(k) # Return the element at the found index, or None if no such element exists result = s[res] if res < len(s) else None print(result) Output:7Explanation:Code imports SortedSet from the sortedcontainers module. This class automatically keeps the elements sorted as they are added or removed.A list a containing integers and a value k are defined. The goal is to find the smallest element in a that is greater than k.List a is converted into a SortedSet named s, which will maintain the elements in sorted order.Method s.bisect_right(k) finds the index of the smallest element in s that is greater than k. This method is similar to a binary search.Using Linear SearchLinear search involves scanning through the list to find the smallest element greater than K .It's a simple and straightforward approach, suitable for small arrays or when sorting isn't necessary.Example: Python # List 'a' and value 'k' a = [1, 4, 7, 5, 10] k = 6 # Initialize 'min_val' to infinity min_val = float('inf') # Find the smallest element greater than 'k' for n in a: if n > k < min_val: min_val = n # Return result: smallest number greater than 'k', or None if not found res = min_val if min_val != float('inf') else None print(res) Output7 Explanation:The variable min_val is initialized to infinity (float('inf')), which acts as a placeholder to ensure that any element in the list will be smaller and can be compared against it.A for loop iterates through each element (num) in the list a. If num is greater than k and smaller than the current min_val, it updates min_val to that element. Comment M manjeet_04 Follow 5 Improve M manjeet_04 Follow 5 Improve Article Tags : Python python-list Python list-programs Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 5 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 2 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 7 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 15+ min read StatsModel Library- Tutorial 4 min read Learning Model Building in Scikit-learn 8 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 7 min read Python | Build a REST API using Flask 3 min read How to Create a basic API using Django Rest Framework ? 4 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like