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.
๐ŸŒ
Enterprise DNA
blog.enterprisedna.co โ€บ python-min-function
Enterprise DNA: We Help Businesses Put Data and AI to Work
A sample of the systems we've shipped and now operate. Real software running real businesses, not demos.
๐ŸŒ
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.
๐ŸŒ
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 โ€บ @ashutosh0626 โ€บ time-complexity-in-python-simply-explained-88b496f29a56
Time Complexity in Python Simply Explained | by Ashutosh Sharma | Medium
April 13, 2023 - Itโ€™s essential to note that the time complexity of a Python program depends not only on the algorithm but also on the input data.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-min-max-value-in-float-string-list
Python | Min/Max value in float string list - GeeksforGeeks
April 8, 2023 - OUTPUT: The min value of list : 4.5 The max value of list : 10.3 ยท The time complexity of this method is O(n) to convert the string list to a numpy array, and O(1) to get the minimum and maximum values using numpy functions.
๐ŸŒ
Programiz PRO
programiz.pro โ€บ resources โ€บ dsa-heapify-complexity
Exploring Time and Space Complexities of Heap Operations
A heap is a binary tree-based data structure where each parent node satisfies a specific order property relative to its children. Its heapify and extract min operations both have a time complexity of O(logโกn).
๐ŸŒ
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 ...
๐ŸŒ
Python
wiki.python.org โ€บ moin โ€บ TimeComplexity
TimeComplexity - Python Wiki
(Well, a list of arrays rather than objects, for greater efficiency.) Both ends are accessible, but even looking at the middle is slow, and adding to or removing from the middle is slower still. See dict -- the implementation is intentionally very similar. As seen in the source code the complexities for set difference s-t or s.difference(t) (set_difference()) and in-place set difference s.difference_update(t) (set_difference_update_internal()) are different!