0Pricing
Coding Interview Prep · Lesson

Find the Index, Not Just the Value

Track positions with enumerate.

Find the Index, Not Just the Value is a free Coding Interview Prep lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Coding Interview Prep learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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

Missing Values Raise Errors

If the value is absent, .index() raises a ValueError, a runtime crash. Check membership first when you are unsure.

if x in a:
    pos = a.index(x)

Loop with enumerate

enumerate hands you the index and value together each step, the clean way to track positions while you scan.

for i, v in enumerate(a):
    print(i, v)

Start enumerate at One

Many judges want 1-based positions. Pass start=1 to enumerate so the first index is 1, matching the problem statement.

for i, v in enumerate(a, start=1):
    ...

Index of the Maximum

To find where the max is, scan and remember the best index, since max(a) alone gives only the value.

best = 0
for i, v in enumerate(a):
    if v > a[best]:
        best = i

A Slick argmax

A compact trick: pair each index with its value and take the max by value. The range gives the position directly.

best = max(range(len(a)), key=lambda i: a[i])

Collect All Matching Indices

Need every position of a value? A comprehension over enumerate gathers all matches in one line.

idx = [i for i, v in enumerate(a) if v == 7]

Map Value to Index

For repeated lookups, build a dict from value to index once, turning each later search into an O(1) hit.

where = {v: i for i, v in enumerate(a)}

Duplicates Need Care

A value to index dict keeps only the last occurrence. Use a list per key, or enumerate, when duplicates matter.

where = {}
for i, v in enumerate(a):
    where.setdefault(v, []).append(i)

Convert Between Index Bases

Mind the base: if you stored 0-based but must print 1-based, add one. Mixing bases is a silent wrong-answer trap.

print(pos + 1)  # 0-based to 1-based

Quick Check

You want 1-based positions while looping. What do you write?

Recap: Track Positions

You can now find single, max, and all indices, and map values to positions safely. enumerate is your reliable position tracker. 🎯

Frequently asked questions

Is the “Find the Index, Not Just the Value” lesson free?

Yes — the full text of “Find the Index, Not Just the Value” is free to read here on the web, and the Coding Interview Prep course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Coding Interview Prep course, upgrade to CoddyKit PRO.

What will I learn in “Find the Index, Not Just the Value”?

Track positions with enumerate. You practise Coding Interview Prep with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Coding Interview Prep?

No prior experience is required. Coding Interview Prep on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Find the Index, Not Just the Value” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Coding Interview Prep lesson?

Yes. Every Coding Interview Prep lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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 Coding Interview Prep