Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Use enumerate to track positions

Find all positions of a target value in any iterable using enumerate for efficient index tracking.

Python practice16 minModules & Standard LibraryIntermediateLast updated March 23, 2026

Problem statement

Given an iterable (string, list, tuple, etc.) and a target value, return a list of all indices where an element equals the target (using ==). Indices should be in ascending order. If the target is not present, return an empty list. Use enumerate to iterate with indices.

Task

Write a function that returns the list of indices where the target appears in the iterable, using enumerate.

Examples

Find letter occurrences in a string

Input

find_indices('abracadabra', 'a')

Output

[0, 3, 5, 7, 10]

Returns each index where 'a' occurs in the string.

Input format

A call: find_indices(iterable, target). The iterable may be any iterable type (string, list, tuple, etc.).

Output format

A list of integer indices where elements equal the target value.

Constraints

Use enumerate to get index and value in a single pass. Equality uses Python's == operator.

Samples

Sample 1

Input

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

Output

[1, 3]

Indices of value 2 in the list are 1 and 3.