Why is this wrong

why is this wrong:

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.

We can use slicing to get the values up to an index lst[0:index] and from an index to the end lst[index+1:]. Also, to append to the end of a list we can use the append() function.
#Write your function here def double_index(lst,index): if index > len(lst): return lst lst[index] = lst[index] * 2 lstnew = lst return lstnew #Uncomment the line below when your function is done print(double_index([3, 8, -10, 12], 2))

Your function didn’t create a new list, it changed the existing list.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.