Advanced Python Code Challenges: Lists, #4: Double Index

Hello,

Could someone explain what I am doing wrong here? I thought it out logically and am stumped at my errors. Thank you!

Challenge:

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.

Code:

#Write your function here
def double_index(lst, index):
  if lst[index] > len(lst):
    return lst
  else:
    replace = (lst[index] * 2)
    new = lst[:lst[index]] + lst(replace) + lst[lst[index] + 1:]
    return new

#Uncomment the line below when your function is done
print(double_index([3, 8, -10, 12], 2))

Error:
"TypeError: List object is not callable

this:

if lst[index] > len(lst):

you still try to access the list by index (lst[index]), so if the index is out of range/bound, you will get an error.

Not sure what you do here:

    replace = (lst[index] * 2)
    new = lst[:lst[index]] + lst(replace) + lst[lst[index] + 1:]

looks really complex, if we know the index, we can simply update the element at that index, why would we need list slicing?

2 Likes