You can define “valid index” as “index which will not raise an error”.

If the list is [10, 20, 30] , then index = 2 would be a valid index; index = 5 would not.

You can define “valid index” as “index which will not raise an error”.

If the list is [10, 20, 30] , then index = 2 would be a valid index; index = 5 would not.

1 Like

I don’t remember anywhere before this problem being told a specific number within the list could be altered, an hour later I finally figured it out

My problem was that I typed:

def double_index(lst, index):

if index < len(lst):

lst = lst[index] * 2

return lst

which correctly returned the list if the index was invalid but returned only the doubled index (in this case -20) if valid.

As I said it took me at least an hour to realise I needed to add “lst[index] = lst[index] * 2” !

Such a simple oversight. So much time wasted.

Can anyone tell me the definition of a valid index in this exercise? index > len(lst), is this valid? I don’t know English, so I use translation software to edit this question, I hope you can understand it.

A list of N elements will have length, N. However, since the index begins with zero, N is said to be *out of range*, so it follows that any index equal to or greater than N will be invalid.

For only positive indices (and 0), x will be less than N.

Eg.

```
if x < len(lst):
# index is valid
```

Thank you very much!

1 Like

You’re welcome.

While it may not have been mentioned or introduced yet, it doesn’t hurt to know that we can read a list from both ends, either left to right (0 to N -1) or from right to left (N - 1 to -N).

That means there are other indices that are valid which are not considered above (or by the exercise author).

```
0 1 2 3 4 5 # positive indices
[ ] [ ] [ ] [ ] [ ] [ ] # list elements
-6 -5 -4 -3 -2 -1 # negative indices
```

Note that lst[2] and lst[-4] are the same element?

So the complete range of valid indices is then,

```
if -N <= x < N:
# index is valid
```

2 Likes

Thank you for your clear and quick reply!

1 Like

I was also completely thrown off by this challenge, but your reply made me understand the solution. Thank you so much.

3 Likes

Thank you so much for posting the question. I was lost as well. I had no idea that “lst” was just abbreviated version of “list”, I read it as the number “1st”. But I get that we can’t use type “list” since its a function.

1 Like

Doesn’t this return the same result?

def double_index(lst, index):

… if index >= len(lst):

… return lst

… else:

… value = lst[index]

… value = value * 2

… lst[index] = value

… return lst

You tell me; does it?

Hi,

I’ve read through all the comments here. I now understand the *= operator and the concept of validating indeces. I came up with this, code, but get the following error:

```
new_lst = lst[index] *= 2
def double_index(lst, index):
if index < len(lst):
return new_lst
else:
return lst
```

Error:

```
File "script.py", line 2
new_lst = lst[index] *= 2
^
SyntaxError: invalid syntax
```

Where am I wrong? Also I don’t understand why the solution below doesn’t use the =* operator, while everybody talks about it in this thread.

```
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
```

In this solution I don’t understand why we slice out 0:index and what the +1 in new_lst = new_lst + lst[index+1:] is for.

Can anyone help? Thanks!!

Hi @core310584058,

The `=`

and `*=`

assignment operators cannot be included in the same line of code. Therefore, the following raises a `SyntaxError`

:

```
new_lst = lst[index] *= 2
```

The correct solution that you posted assembles the result in three parts.

This first part provides the portion of the `list`

that precedes the affected index:

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

This second part performs the multiplication by `2`

and `append`

s it to the accumulating result:

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

This third part uses `[index+1:]`

to retrieve and assign the slice that begins with the item that is right after the affected index and includes everything from there to the end of the `list`

:

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

2 Likes

Thank you @appylpye! Now it makes sense. One of the reasons I didn’t understand was because I overlooked the “:” after “+1”.

But why do we need to go through all these to understand this concept? I honestly think Code Academy need to review her lesson material and the order with which the challenges come. I am getting so frustrated by the day. Yet, despite complaint from all corners, there is no change. Why not save the institution the rigor of trying to explain over and over again? why not save the students the time to try to read bunch of comments, get confused, understand, get confuse again etc? The easier way is always better. I hope i will eventually understand after reading comment for hours!!! SMH

**Question 1**

Can you please point to where there is any lesson on how to double index before now? It is possible that some of us actually missed it. Thank you

```
**Question 2**
With reference to the below code (as copied from codeacademy solution), Can anyone please help me to understand this code and explain it in a very simple terms. I can see the reason wht i should have >= after the "if" statement. Why not just ">". Why do we need to have new_lst = new_lst + lst[index+1:].....what exactly is the + operator doing 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], 2))
```

I do not doubt the quality of the code and i am pretty sure that my confusion is due to the yet low level of my understanding of python, I will however appreciate codeacademy look into its material and help strugglers like me understand better. The essence of learning is to know and not to get confused and demotivated

The above code is adding to the list, rather than accessing the value at the supplied index position, doubling the value, then assigning it back to that position.

```
lst[index] *= 2
```

or,

```
lst[index] = lst[index] * 2
```

Eg.

```
[3, 8, -10, 12]
^
index = 2
lst[index] *= 2
[3, 8, -20, 12]
^
value at index is doubled
```

1 Like

No one should be mad at you at all. You are doing exceptionally well to help the students. Most are frustrated with the method codeacademy uses. I honestly think what we are going through in this lesson can be avoided. You will realize that you go through the stress of explaining over and over again. Trust me and for fact, not all student will read all comments here. So, you might continue to get questions you have already answered. Again mtf, you have been exceptional and we can do better here. If most are complaining and yet there is nothing from codeacademy to effect some corrections, then it seems strange

2 Likes

Hello @sodiqolabodeafolayan.

I doubt there is a lesson that specifically covers doubling an element at a specific index in a list, but you presumably have learned about lists before encountering this challenge. Accessing an element of a list by its index should be familiar to you. All this challenge is asking us to do, is locate the element at the index specified by the index parameter, and double its value.

As to the Codecademy solution, it builds a new list in three parts. First it takes a slice of the original list up to the specified index, then it appends the doubled value of the element at the specified index, then it uses `+`

to concatenate the slice of the original list from right after the specified index through the end of the list.

```
new_lst = lst[0:index] #slice of the beginning of the list up to the index specified
new_lst.append(lst[index]*2) #append the doubled value of the element at the specified index
new_lst = new_lst + lst[index+1:] #concatenate the remaining elements from the original list
return new_lst #return the new list
```

In the code you posted, the list has 4 elements. That means `len(lst)`

is 4, but the index values are 0, 1, 2, 3. There is no element at index 4, so if the `index`

is greater than or equal to the length of the list, we return the original list.

Thank you for the explanation.

2 Likes