Functions Index exercice help

https://www.codecademy.com/courses/learn-python-3/articles/advanced-python-code-challenges-lists
exercice 4 (Double Index)
Create a function named double_index that has two parameters: a list named lst and a single number named index .

The function should return a new list where all elements are the same as in lst except for the element at index . The element at index should be double the value of the element at index of the original lst .

If index is not a valid index, the function should return the original list.

For example, the following code should return [1,2,6,4] because the element at index 2 has been doubled:

double_index([1, 2, 3, 4], 2)

After writing your function, un-comment the call to the function that we’ve provided for you to test your results.

What i wrote :

def double_index(lst, index):

if index > len(lst):

return lst

else:

lst[index] = lst[index]*2

return lst

print(double_index([3, 8, -10, 12], 2))

It works but for some reason the exercice says I’m wrong. Can someone explain me ? Thanks ! :slight_smile:

Hey @code4843959602! Welcome to the forums :slight_smile:

With your actual code, try passing an index of 4 instead of 2.

len(lst) will return how many elements there are in the list, which means, the last index + 1. For example, the list you used had 4 elements. The index of the last one is 3. And len(lst) will return 4. Therefore you’re saying:

If the index is bigger than 4, then just return the original list.

When you should be saying:

If the index is bigger than or equal to 4, then just return the original list.

So I think this should do:

if index >= len(lst):

return lst

Really hope this helped! :smiley: