Why does the +1 in the solution remove a number from the printed list?

Code Challenge: Lists, 2. Double Index

Line 8 has [index+1:]

#Write your function here
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

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

I am trying to understand why the +1 would remove an item from the printed list.

If the +1 is removed from line 8:

#Write your function here
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:]
    return new_lst

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

You get:
[3, 16, 8, -10, 12]

Why does the +1 remove the ‘8’ from the printed list and leave it if not entered?

Thank you in advance! this has confused me the last couple days.

Hey @morningtheft

Welcome to the forum. Might I suggest that, if you’re stuck with this, you go back and review the Lists content - specifically around slicing.

To understand what’s happening, look at each line individually.

Our function call is double_index([3, 8, -10, 12], 1). Here’s what Python is doing.

Step 1: Evaluate if index >= len(lst):

index = 1, and len(lst) = 4, so this evaluates to False and we run the code in the else: block.

Step 2: Run the else: block.

Step 2.1: new_lst = lst[0:index]

To start with, lst = [3, 8, -10, 12] and index = 1. These are the arguments we passed into the function.

What we’re doing is we’re taking a slice of lst, and assigning it to new_lst.

lst[0:index] is the same as lst[0:1], so we slice out every item in the list from index 0 up to but not including index 1. lst[0:1] = [3], so new_lst = [3].

Step 2.2: new_lst.append(lst[index]*2)

Now, we need to evaluate lst[index]*2. index is still 1, so lst[index] is lst[1] = 8.

Next, we multiply that value by 2 - so we get 16 - and we append this to new_lst.

The result is that new_lst = [3, 16].

Step 2.3: new_lst = new_lst + lst[index+1:]

Here, we’re simply concatenating the existing new_lst with another slice from lst.

Lets work out the slice first.

We still know that index = 1, so index+1 = 2. We can then work out that lst[index+1:] is the same as lst[2:], so we’re taking a slice of every item in lst from index 2 to the end of the list. lst[2:] = [-10, 12], so when we concatenate them we get new_lst = [3, 16] + [-10, 12] = [3, 16, -10, 12].

Step 2.4: return new_lst.

We return the value [3, 16, -10, 12] to our print() call, and so you get the output [3, 16, -10, 12] in the console.

Hopefully, this is now obvious, but just in case it isn’t…

Removing the +1 means that the slice we are adding on to the end of new_lst includes the list item we’re doubling.

The value of index never changes from 1. If our slice is lst[index:], we get [8, -10, 12]. This is wrong, because our goal is to take a list and double the value at a specific index in that list. Without the +1, what we’re doing is injecting an additional value - of double the target index - into the list at that position.

Does that clarify it at all? :slight_smile:

2 Likes