Find the Index, Not Just the Value
Track positions with enumerate.
Positions Matter
Often the answer is not the value but where it sits. Contest tasks frequently ask for a 1-based or 0-based index, so track positions.
a = [4, 1, 7, 3]Find with .index()
a.index(x) returns the position of the first match. It is quick to write but scans left to right in O(n).
pos = a.index(7) # 2All lessons in this course
- Lists, Indexing & Slicing for CP
- Build Arrays Fast with Comprehensions
- Min, Max, Sum & Running Totals
- Find the Index, Not Just the Value