Remove the First Occurrence

Easy ⏱ 8 min 85% acceptance ★★★★★ 4.9
Write remove_first(items, value) that removes only the first occurrence of value from items using .remove() and returns the list. If value is not present, return the list unchanged.

Examples

Example 1
Input
items = [1, 2, 3, 2, 1], value = 2
Output
[1, 3, 2, 1]
Explanation

Only the first 2 (at index 1) is removed.

Example 2
Input
items = [1, 2], value = 9
Output
[1, 2]
Explanation

9 is not present, so nothing changes.

Constraints

  • 0 <= len(items) <= 10^4

Topics

ListsMutation

Companies

WalmartHCLTech

Hints

Hint 1

.remove(value) raises ValueError if value is missing — guard with an `in` check first.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.