Find Minimum Value of an Array in Python
What you’ll learn
- What minimum means in simple terms: smallest value in the list.
- How a one-pass running minimum algorithm works.
- Two Python examples, including all-negative values, plus live preview.
Overview
To find the smallest value, scan from left to right and keep the smallest seen so far in one variable.
Two examples
Mixed values and all-negative values.
Live preview
Uses the same sample list as code example 1.
Interview tip
Mention O(n) scan and empty-list safety.
Prerequisites
Basic Python loops and list indexing.
- Know list syntax like
[12, 5, 7, 3, 2, 8, 10]. - Understand that numbers can be negative, and comparisons still work.
The idea
Use the first list value as current minimum. For each next value, if it is smaller, update the current minimum.
After the final element, the current minimum is the answer.
Live preview
Same sample list as example 1: 12, 5, 7, 3, 2, 8, 10.
Algorithm
Goal: return the smallest value from a list of n integers.
Validate input
If list is empty, report error first.
Initialize minimum
Set min_val = arr[0].
Scan remaining values
If a value is smaller than min_val, update it.
Return answer
Return final min_val.
📜 Pseudocode
function find_min(arr):
if arr is empty:
error
min_val <- arr[0]
for each value from second element:
if value < min_val:
min_val <- value
return min_val Find minimum (reference program)
Same sample values as reference flow; output is 2.
def find_min(arr: list[int]) -> int:
if not arr:
raise ValueError("Array must contain at least one element.")
min_val = arr[0]
for i in range(1, len(arr)):
if arr[i] < min_val:
min_val = arr[i]
return min_val
def main() -> None:
array = [12, 5, 7, 3, 2, 8, 10]
min_value = find_min(array)
print(f"Minimum value in the array: {min_value}")
if __name__ == "__main__":
main() When every element is negative
Smallest means most negative, so answer is -9 here.
def find_min(arr: list[int]) -> int:
if not arr:
raise ValueError("Array must contain at least one element.")
min_val = arr[0]
for x in arr[1:]:
if x < min_val:
min_val = x
return min_val
def main() -> None:
negatives = [-9, -3, -1, -7]
print(f"Minimum (most negative): {find_min(negatives)}")
if __name__ == "__main__":
main() Notes
Built-in. Python has min(arr), but manual scan is often required in interviews.
Parallel scan. Large systems can compute partial minima and combine them.
❓ FAQ
🔄 Input / output
You can collect numbers from input, store in a list, and pass that list to find_min.
Edge cases
Empty list
There is no minimum; validate before reading first element.
One element
The minimum is that one value.
⏱️ Time and space complexity
| Approach | Time | Extra space |
|---|---|---|
| Single scan | O(n) | O(1) |
Summary
- Algorithm: running minimum in one pass.
- Cost:
O(n)time andO(1)extra space. - Important: define behavior for empty input.
Finding the smallest value in an unsorted list takes a single left-to-right pass: O(n) time and O(1) extra memory. In the worst case, any unseen element could still be the new minimum.
8 people found this page helpful
