0Pricing
Competitive Programming Academy · Lesson

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)  # 2

All lessons in this course

  1. Lists, Indexing & Slicing for CP
  2. Build Arrays Fast with Comprehensions
  3. Min, Max, Sum & Running Totals
  4. Find the Index, Not Just the Value
← Back to Competitive Programming Academy