Advanced Python Code Challenges: Lists

Lists
I am struggling with this question. Can someone help me with the code:

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.
Link
https://www.codecademy.com/courses/learn-python-3/articles/advanced-python-code-challenges-lists
Q4

what code do you have so far? What are you struggling with?

You learn most if we guide you, but you have to solve the problem in the end :slight_smile:

I’m having the same difficulty with this question. I just cant think of a way to modify and index within a list and also check if the index is valid?
I’ve done it but I feel like the code is sketchy and not very robust.

#Write your function here
def double_index(lst,index):
  if index <= len(lst)-1:
    x = lst[index] * 2
    lst2 = lst[index+1:]
    return lst[:index] + [ x ] + lst2
  else:
    return "You're index is too big"
  

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

you can check if the index is valid first? If the index isn’t valid, you don’t need to modify the list.

why use list slicing? We can update an element in a list if we know the index in a much simpler way.