Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Find element index or return -1

Find the first index of a target in a list, or return -1 if it isn't present.

Python practice10 minLists & TuplesBeginnerLast updated March 17, 2026

Problem statement

Write a function find_index_or_minus_one(lst, target) that searches lst for target and returns the index of the first occurrence. If target is not present, return -1. Do not raise exceptions for missing elements.

Task

Implement a function that returns the index of the first occurrence of target in a list, or -1 when the target is not found.

Examples

Find existing element

Input

find_index_or_minus_one([1, 2, 3], 2)

Output

1

2 is at index 1 in the list.

Input format

A function call: find_index_or_minus_one(lst, target)

Output format

Return an integer index (or -1 if not found).

Constraints

Use linear search (O(n)). The list may contain mixed types; equality comparison should be used.

Samples

Sample 1

Input

find_index_or_minus_one([], 'x')

Output

-1

Empty list can't contain 'x', so return -1.