 # Double Index: Advanced Python Code Challenges

TBH, the Double Index challenge was too advanced for me.
Advanced Python Code Challenges: Lists

``````def double_index(lst, index):
if index >= len(lst):
return lst
else:
new_lst = lst[0:index]
new_lst.append(lst[index]*2)
new_lst = new_lst + lst[index + 1:]
return new_lst
print(double_index([3, 8, -10, 12], 2))
``````

So I did a research of why the code is as follows. I decided to make sense of each and every line:

index represents the position of values in the lst>>>

``````def double_index(lst, index):
``````

so, if index number value is bigger than the len of the list which has 4 numbers [3, 8, -10, 12], original list remains >>>

``````  if index >= len(lst):
return lst
``````

Or else, returns a new list with positions 0 to index where INDEX is the POSITION assigned - in this case 3rd position = 2. This new_lst is assigned the slice beginning at index 0 up to, but not including index 2. The value assigned to new_lst now is [3, 8].>>>

``````  else:
new_lst = lst[0:index]
``````

Append double the value at index. That line appends the doubled value of the element - 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]. It doesn’t REPLACE, because we already cut the slice UNTIL index 2, so new double value appears there!

``````  new_lst.append(lst[index]*2)
``````

Adds the rest of the original list, from 1 on… And adds lst[index + 1:] == which is  . 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, already in lst[0:index]. Index is the index of the element position 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. When we concatenate the values: new_lst = [3, 8, -20] +  we get [3, 8, -20, 12] as the value assigned to new_lst which is then returned.>>>

``````  new_lst = new_lst + lst[index + 1:]
return new_lst
``````

I would not have done this code on my own, even though I practice a lot on each and every lesson.
I guess with time, and learning the rules and logic of the language it will be easier.
If i made a mistake in my conclusions (which I also gathered from research), please correct. 3 Likes

Seems like a good breakdown, nice one. The docs have a helpful way to think about slicing indices if it still seems a little weird.

From https://docs.python.org/3/tutorial/introduction.html#strings-
"One way to remember how slices work is to think of the indices as pointing between characters, with the left edge of the first character numbered 0. Then the right edge of the last character of a string of n characters has index n , for example:

`````` +---+---+---+---+---+---+
| P | y | t | h | o | n |
+---+---+---+---+---+---+
0   1   2   3   4   5   6
-6  -5  -4  -3  -2  -1
``````

The first row of numbers gives the position of the indices 0…6 in the string; the second row gives the corresponding negative indices. The slice from i to j consists of all characters between the edges labeled i and j , respectively."

3 Likes

I was doing a simples review on the pratice list, and this appeared. i was like " what kind of sorcery would they want me to do here?" I managed to get the 1st part, but couldn’t go further. Glad i found this. Thank You, and it makes sense in fact, the thing is, how should i reach here…

While learning, I think it’s incredibly valuable for one to write out exactly what the code is doing at specific steps in order to understand what it’s doing. Write it out in language that you understand (like you did above with this function). I use comments in my Colab Notebook when going through the modules and I take notes as well.

Good work! 1 Like