I still need help. I decided to retake this course and ensure that i have a very sound comprehension of every concept and exercise before moving forward.

Kindly see my understanding of the solution to the below code challenge

def double_index(lst, index):
if index >= len(lst):
return lst
else:
new_lst = lst[0:index]
#the above line assign `index` as one of the indexes of the list, right?
new_lst.append(lst[index]*2)
#the above line append double `index`
new_lst = new_lst + lst[index+1:]
#I dont understand the reason why i need to add 1 to the already doubled index. I am sure it wont be added if not necessary but obviously the limitation of my knowledge is not helping here. Please shed more light
return new_lst
#Uncomment the line below when your function is done
print(double_index([3, 8, -10, 12], 2))

Please note, i wont proceed without having a good understanding:slightly_smiling_face:

No. That line is taking a slice of the original list, and assigning it to the new list. Using the arguments in the function call you’ve included from the exercise:

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

new_lst is assigned the slice beginning at index 0 up to, but not including index 2.
So, the value assigned to new_lst is [3, 8].

That line appends the doubled value of the element in the original list located at index 2.
The value of the original list at index 2 is -10, so -20 is appended.
Now the value of new_lst is [3, 8, -20].

This line is concatenating a slice of the original list to new_lst. The slice needs to include the elements from index 3 through the end of the list since we already have the elements at index 0, index 1 and index 2. The value of our parameter index is still 2. index is the index of the element we wanted to double not the value of the element. To get the remainder of the list concatenated to our new_lst we start our slice at the element after index 2 by adding 1 to index.
The value of lst[index + 1:] is [12].

When we concatenate the values: new_lst = [3, 8, -20] + [12] we get [3, 8, -20, 12] as the value assigned to new_lst which is then returned.

Here is the solution code that you posted with several print statements added to verify what is going on:

def double_index(lst, index):
if index >= len(lst):
return lst
else:
print(f'lst[0:index] >>> {lst[0:index]}')
new_lst = lst[0:index]
print(f'new_lst >>> {new_lst}')
#the above line assign `index` as one of the indexes of the list, right?
print(f'lst[index] * 2 >>> {lst[index] * 2}')
new_lst.append(lst[index]*2)
print(f'new_lst >>> {new_lst}')
#the above line append double `index`
print(f'lst[index + 1:] >>> {lst[index + 1:]}')
new_lst = new_lst + lst[index+1:]
print(f'new_lst >>> {new_lst}')
#I dont understand the reason why i need to add 1 to the already doubled index. I am sure it wont be added if not necessary but obviously the limitation of my knowledge is not helping here. Please shed more light
return new_lst
#Uncomment the line below when your function is done
print(double_index([3, 8, -10, 12], 2))

I have read it all and i can say i have around 80% understanding of it. I believe i can move forward now . Clearly, i wouldn’t have been able to come up with such kind of solution at this stage of my learning. I am convinced, i will do better over time.

However, i will like to point out that my mindset in approaching the code challenge might be the cause of my limitation. I will use this as an example and i will appreciate your clarity.

I approached the challenge with the assumption that i do not know what the the index will be and that my solution has to be such that whatever index given, the code should be able to handle it. However, reading the solution by codeacademy and your excellent explanation, it seems you worked with the assumption that you already know what the index is. Please kindly correct me if i am wrong on this assumptions.

My question is, based on the two assumptions, how should i approach the challenge?

I was not assuming that we know what the index is. I was just using the argument given in the ‘test case’.
The line that the exercise asks us to uncomment has an array: [3, 8, -10, 12] and an index: 2. These values are used to test our function, or in this case the function provided by the Codecademy solution. You could try it with any array, and any index to see the same steps are taking place. Here’s an example:

def double_index(lst, index):
if index >= len(lst):
return lst
else:
print(f'lst[0:index] >>> {lst[0:index]}')
new_lst = lst[0:index]
print(f'new_lst >>> {new_lst}')
#the above line assign `index` as one of the indexes of the list, right?
print(f'lst[index] * 2 >>> {lst[index] * 2}')
new_lst.append(lst[index]*2)
print(f'new_lst >>> {new_lst}')
#the above line append double `index`
print(f'lst[index + 1:] >>> {lst[index + 1:]}')
new_lst = new_lst + lst[index+1:]
print(f'new_lst >>> {new_lst}')
#I dont understand the reason why i need to add 1 to the already doubled index. I am sure it wont be added if not necessary but obviously the limitation of my knowledge is not helping here. Please shed more light
return new_lst
#Uncomment the line below when your function is done
print(double_index([20, 40, 60, 80, 100, 120], 1)) #should see 40 get doubled

You could try running the above example yourself, and change the index from 1 to any of the other index values (0 - 5). Try all of them, and see if it works every time. If it does, great! If not, we have more to do.