Insert a Value at a Position

Easy ⏱ 8 min 73% acceptance ★★★★★ 4.9
Write insert_value(items, index, value) that inserts value into items at position index using .insert() and returns the mutated list. If index is greater than the list length, the value should end up at the end (Python's default .insert() behavior already does this).

Examples

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

3 is inserted before the element currently at index 2.

Constraints

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

Topics

ListsMutation

Companies

PayPalRazorpay

Hints

Hint 1

list.insert(index, value) shifts later elements right.

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