Lesson guide
What this Python exercise practices
Find element index or return -1 is a beginner practice lesson that focuses on lists, iteration, filtering. It is designed to be solved in about 10 minutes with examples, starter code, and test feedback.
Prerequisites
- Python variables
- List values
- Basic indexing
Difficulty and time
- Level
- Beginner
- Estimated time
- 10 minutes
Practice path
Summary
Find the first index of a target in a list, or return -1 if it isn't present.
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
Explanation
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 input 0
find_index_or_minus_one([], 'x')
Sample output 0
-1
Explanation 0
Empty list can't contain 'x', so return -1.
AI assistant
Ask me anything!
Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.
Chat history is temporary and will not be saved.
Free preview includes 1 Teach Theory response and 1 AI hint per unlocked preview lesson.