Finding the index of all occurrences of a specific element in a list can be achieved using a loop or list comprehension. Here are two examples:
Example 1:
numbers = [2, 3, 4, 2, 5, 2] target = 2 indices = [] for i in range(len(numbers)): if numbers[i] == target: indices.append(i) print(indices)
Output:
[0, 3, 5]
Example 2:
numbers = [2, 3, 4, 2, 5, 2] target = 2 indices = [i for i in range(len(numbers)) if numbers[i] == target] print(indices)
Output:
[0, 3, 5]