In the first block of code the block of code runs the min function over the whole array, which takes O(n) time. Now considering it is in a loop of length n then the total time is O(n^2)

Looking at the 2nd block of code. Note that the min function is only comparing 2 values, which is arguably O(1). Now considering that it is in a loop of length n. The total time is simply the summation of O(n+n+n), which is equal to O(n)

Answer from March_G on Stack Overflow
๐ŸŒ
Codemia
codemia.io โ€บ home โ€บ knowledge hub โ€บ big o of min and max in python
Big O of min and max in Python | Codemia
September 24, 2025 - The time complexity of Python's built-in min() and max() is generally O(n) for iterables, because each element must be examined at least once to guarantee...
๐ŸŒ
Reddit
reddit.com โ€บ r/leetcode โ€บ is the min() function considered to be o(1) in python?
r/leetcode on Reddit: Is the min() function considered to be O(1) in python?
May 2, 2023 -

Hi! I am having a hard time understanding if the builtin min function in python is of time complexity O(1). Since it might have to go through the list of elements, won't it be O(n) instead. I have seen another solution where they used 2 seperate stacks for maintaining stack and min_element. Is that a better way to do it to ensure that all methods are of O(1) time complexity? Any help is appreciated!

This is the problem I solved recently: 155. Min Stack (LC)

๐ŸŒ
Pythoncomplexity
pythoncomplexity.com โ€บ builtins โ€บ min
Minimum - Python Big-O: Time & Space Complexity
# O(n*k) - evaluates key for each item words = ["apple", "pie", "cat"] shortest = min(words, key=len) # "pie" (3 letters) # More expensive key function numbers = [1, 2, 3, 4, 5] min_val = min(numbers, key=lambda x: expensive_function(x)) # O(n*k) where k = time for expensive_function ยท # O(n) - makes one pass through data lst = list(range(1000000)) result = min(lst) # ~1M comparisons # Same complexity with generators result = min(x**2 for x in range(10000)) # O(n) time, O(1) space
๐ŸŒ
Reddit
reddit.com โ€บ r/python โ€บ time complexity of min(set, function)
r/Python on Reddit: Time complexity of min(set, function)
March 11, 2017 -

I'm implementing an algorithm and I need a data structure with both very fast lookup of arbitrary elements like you get from a hash table and similar to a priority queue very fast lookup of the highest priority element ordered by a key associated with each item.

Is there anyway I can accomplish this? I thought of just using a set and something like min(set, lambda x: x.key), would this have to iterate through all elements?

๐ŸŒ
Sean Coughlin
blog.seancoughlin.me โ€บ mastering-the-minstack-efficiently-supporting-minimum-element-retrieval-in-constant-time
Efficient MinStack: Constant Time Min Element Retrieval
June 28, 2024 - The MinStack problem requires creating a stack data structure that supports the following operations, all in constant O(1) time: ... MinStack() initializes the stack object. void push(int val) pushes the element val onto the stack. void pop() ...
Find elsewhere
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ python-how-to-use-default-value-in-min-function-419879
How to use default value in min function | LabEx
The min() function has a time complexity of O(n) for iterables, making it efficient for most use cases in LabEx programming environments. ... By understanding these basics, you'll be well-prepared to use the min() function effectively in your ...
๐ŸŒ
Scribd
scribd.com โ€บ document โ€บ 730160411 โ€บ H-Standard-Algorithms
Min Time Complexity in Python Algorithms | PDF | Control Flow | Time Complexity
H - Standard Algorithms - Free download as PDF File (.pdf), Text File (.txt) or view presentation slides online.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ complexity-cheat-sheet-for-python-operations
Complexity Cheat Sheet for Python Operations - GeeksforGeeks
July 12, 2025 - Python built-in data structures like lists, sets, and dictionaries provide a large number of operations making it easier to write concise code However, not understanding the complexity of these operations can sometimes cause your programs to run slower than expected.
๐ŸŒ
Medium
medium.com โ€บ @kapildevkhatik2 โ€บ how-about-python-optimization-mastering-time-and-space-complexity-for-improved-performance-e094101b42f8
Python Optimization: Mastering Time and Space Complexity for Improved Performance - Techniques for Enhanced Efficiency | Medium
June 18, 2023 - Here are some techniques for improving the time complexity of your Python code: Use built-in functions: Python provides many built-in functions that are optimized for efficiency. For example, instead of looping through a list to find the minimum value, you can use the min function.
๐ŸŒ
Medium
medium.com โ€บ @ashutosh0626 โ€บ time-complexity-in-python-simply-explained-88b496f29a56
Time Complexity in Python Simply Explained | by Ashutosh Sharma | Medium
April 13, 2023 - In Python programming, complexities refer to the amount of time and resources required to execute an algorithm or perform a certainโ€ฆ
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-min-max-value-in-float-string-list
Python | Min/Max value in float string list - GeeksforGeeks
April 8, 2023 - The original list is : ['4.5', '7.8', '9.8', '10.3'] The min value of list : 4.5 The max value of list : 10.3 ยท Time complexity: O(n), where n is the length of the list.
๐ŸŒ
Programiz PRO
programiz.pro โ€บ resources โ€บ dsa-heapify-complexity
Exploring Time and Space Complexities of Heap Operations
Therefore, the overall time complexity of extracting the min element from a heap is O(logn).
๐ŸŒ
LeetCode
leetcode.com โ€บ problems โ€บ interleaving-string โ€บ solutions โ€บ 1717669 โ€บ Python:-Space-complexity:-O(min(mn))-Time-Complexity:-O(mn)
Interleaving String - LeetCode
January 25, 2022 - Can you solve this real interview question? Interleaving String - Given strings s1, s2, and s3, find whether s3 is formed by an interleaving of s1 and s2. An interleaving of two strings s and t is a configuration where s and t are divided into n and m substrings respectively, such that: * s ...
๐ŸŒ
ESS Institute
essinstitute.in โ€บ home โ€บ blog โ€บ introduction to time complexity in python
Introduction to Time complexity in Python | ESS Institute
June 3, 2023 - Itโ€™s is a standard method for estimating time complexity. Itโ€™s usually expressed in big-O notation, which represents the upper bound of the worst-case scenario. Big-O notation helps us to compare the performance of different algorithms and data structures in terms of their scalability. Python counts the number of operations a data structure or method needs to perform in order to take a given task to end.