easy +10 pts

Last occurrence index

Find the index of the last occurrence of a value in a list, or -1 if absent.

Write a function `last_index(lst, value)` that takes a list `lst` and a value `value` and returns the index of the **last** occurrence of `value` in `lst`. If `value` is not present, return `-1`. Indices are zero-based. The list may contain any type of elements; equality is standard Python equality (`==`). Handle empty lists and single-element lists correctly.

Constraints

- The list can be empty. - The list may contain any hashable or unhashable elements? (Actually unhashable elements like lists are allowed; equality is based on `==`). - If the value appears multiple times, the largest index is returned. - The implementation should not modify the input list. - Time complexity: O(n), space: O(1).

Example

```python
>>> last_index([1, 2, 3, 2, 1], 2)
3
>>> last_index([1, 2, 3], 9)
-1
>>> last_index([], 5)
-1
>>> last_index(["a", "b", "a"], "a")
2
```
10 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Iterate through the list from the end to the beginning to find the last occurrence quickly.
You can also iterate from the start and update a variable whenever the value matches.
Remember to return -1 if no match is found.
Edge case: an empty list should return -1.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.