Python: Lists - Double Index HELP! SOS!


I really need help understanding this exercise. I am completely lost to what is happening in the syntax. I’ve done well up until this point. I need someone to explain to what is happening in this code from start to bottom and thoroughly.

This is the solution to the portal, however I don’t get what is happening after the if statement.

  1. What is happening when we use lst[index] as a variable?
  2. Why are we using the index inside of which are meant for list (if I am correct)?


#Write your function here
def double_index(lst, index):
if index < len(lst):
lst[index] = lst[index] * 2
return lst

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

Lists - Double Index


It is not a variable so much as a reference to the index of the list lst that is passed to the function. The element at that index is then multiplied by two and stored at the same index.

When print the returned list, it will be,

[3, 8, -20, 12]

Notice that the third value in the list (index[2]) is now -20 from the previous -10.


The square brackets [ ] are known as the subscript. It is how we access the element (value) at position index in the list, lst.


You’re awesome, Thank you very much for that in-depth explanation. I understand much better now. :heart: