Index of Minimum Element
Find the zero-based index of a list's minimum with Python. Students update a value-index pair and explain why repeated minima keep the first position.
1# Take array2n = int(input("Enter array size: "))3arr = []4for i in range(n):5 arr.append(int(input(f"Element {i+1}: ")))67# Find index of min8min_index = 09min_value = arr[0]1011for i in range(1, len(arr)):12 if arr[i] < min_value:13 min_value = arr[i]14 min_index = i1516print(f"Index of minimum: {min_index}")
Output
Enter array size: 5 Element 1: 10 Element 2: 5 Element 3: 20 Element 4: 3 Element 5: 15 Index of minimum: 3
In this one
- Track minimum positions
- Retain first tied minima
- Require nonempty data
How it works
- 1
Initialize the minimum value and index from position zero.
- 2
Inspect each later indexed value.
- 3
Replace both fields only for a smaller candidate.
Edge cases
Which repeated minimum is reported?
Which repeated minimum is reported?
The first one, because equality does not update the pair.
Can an empty list be scanned this way?
Can an empty list be scanned this way?
No. The initial arr[0] access fails.
What's going on
min_index starts at zero and min_value at arr[0]. Each later index supplies a candidate value.
Both variables update only when a strictly smaller value appears. Repeated minima therefore preserve the earliest index. Tracking the index alongside the value avoids a separate lookup pass, but initialization requires at least one element.
Which repeated minimum is reported? The first one, because equality does not update the pair.
Can an empty list be scanned this way? No. The initial arr[0] access fails.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What must update with min_value?
Pick an answer to check it.
Question 2
Which repeated minimum is reported?
Pick an answer to check it.
Questions people ask
Which repeated minimum is reported?
The first one, because equality does not update the pair.
Can an empty list be scanned this way?
No. The initial arr[0] access fails.
What must update with min_value?
min_index.
๐Related Content
- ๐
Phase 4 - Learn Concepts
Review Phase 4 concepts and explanations
- ๐
Phase 4 - Quiz
Test your Phase 4 understanding with quiz questions
- ๐ป
Phase 4 - All Practice Problems
Explore all practice problems for Phase 4
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum