Skip to contentKEEP THE CRAFTTHE SANCTUARY

Find the First Matching Index

Scan a list once and return the index of the first value equal to the target.

The problem

Implement first_index(values, target). Return the smallest index i where values[i] == target, or -1 if target does not appear in values. Write the loop yourself: do not use list.index.

Starter code

def first_index(values, target):
    pass

Public examples

  1. values = [4, 2, 4, 7], target = 4gives0
  2. values = [1, 3], target = 2gives-1

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.