# What does index of lst even mean?

Early in the lessons on list objects we learn how to access the elements by their index.

``````lst = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
``````

The first element is at index 0, so,

``````lst[0] == 1              # True
``````

The length is always 1 greater than the last index within range, so,

``````n = len(lst)

lst[n - 1] == 55         # True
``````

Using negative indices we can access the list from the right side, where,

``````lst[-1] == lst[n - 1]    # True
``````

Given a random index, we need to check that it is in range (that is, valid, and which can be accessed in the list).

``````if index < len(lst)
``````

when True means there is an element that corresponds to that index.

Taking into account negative indices, `-n` is a valid index…

``````lst[-n] == lst[0]        # True
``````

So all indices between and including `-n` and `n - 1` are valid.

`````` o--o--o--o--o--o--o--o--o--o--o--o
-n             -1  0              n-1
``````

Below we see how the two correspond…

`````` o--o--o--o--o--o
-n             -1
|              |
o--o--o--o--o--o
0              n-1
``````

For this exercise we need only concern ourselves with positive indices, or zero since the SCT does not test with any negative values for `index`.

``````if index < n:
``````

is sufficient to the requirements.

In the above list, `n` is 10 so an `index` of `0` to `9` is valid. Given that index, we can access the corresponding element in the list. let `index` equal `9`

``````lst[index] == 55    # True
``````

Modifying the element is a simple matter of assigning a new value to that element. In this case, we are taking the existing value and doubling it, then assigning the new value back to that element.

``````lst[index] = lst[index] * 2
new          old
value        value

lst[index] == 110    # True
``````

Polling the list, gives,

``````[1, 1, 2, 3, 5, 8, 13, 21, 34, 110]
``````

When you complete the lesson and your code is accepted,

``````n = len(lst)
if index < n:
``````

can become,

``````n = len(lst)
if index in range(-n, n):``````
4 Likes

Thanks for explaining all this! I think I understand it better now. But I feel like this lesson has gone from easy to hardcore in one module. So is “index” and “element” interchangeable terms in some cases?

They are related, but I wouldn’t say interchangeable. `element` is a generic term referring to the list cell. Think of it as a value container. A list contains len(list) elements. We access those elements specifically by their `index`.

=> elements in a list

The checked element is at index 4.

OK got it, Thanks! I am going to review the “working with lists” section again before moving on.

1 Like

I just wanted to say that I had the same problem as some people above, I could not get this to work because I didn’t know (and didn’t experiment maybe?) that I could modify an existing element of a list… After I found that out it was easy.

I agree with everyone saying this was too advanced for the stuff we learned. It might make sense to the seasoned python people in here but when you are just a beginner, baby steps are needed.

1 Like

def double_index(lst, index):
if (index >= 0 and index <= len(lst)-1) or (index < 0 and index >=-len(lst)):
return lst[index]*2
else:
return lst
#Uncomment the line below when your function is done
print(double_index([3, 8, -10, 12], 0))
print(double_index([3, 8, -10, 12], 1))
print(double_index([3, 8, -10, 12], 2))
print(double_index([3, 8, -10, 12], 3))
print("----------------------------")
print(double_index([3, 8, -10, 12], -1))
print(double_index([3, 8, -10, 12], -2))
print(double_index([3, 8, -10, 12], -3))
print(double_index([3, 8, -10, 12], -4))
print("-------Going out of bounds-------")
print(double_index([3, 8, -10, 12], -5))

Can you guys tell me what’s wrong here? I keep getting a message saying " Double the value at position `index` in `lst`"

It prints exactly what it should. It doubles all the values whether the index given is a positive position or negative. Also i’ve noticed if i do -0 it also “works”. Is that even accepted at all?

Hey everyone! This is the code I used:

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

The solution doesn’t want to accept it even though the print statement yields the correct values. (I did indent btw)

Hi @sig94,

Check your `double_index` function by calling it with this line:

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

That will only return the doubled value from the list, not change the value in the list.

``````if index < len(lst):
lst[index] *= 2
return lst
``````

No else needed, either. We carry out the change, if allowed, then return the list in either case.

Thank you!

1 Like

I don’t understand the solution; in particular, I don’t understand the line

lst[index] = list[index]*2

I’m reading it like an equation that is flatly false:
x=2x

I figured we’d need to set a new list (for instance, new_lst) and set the element new_lst[index] equal to list[index]*2, then print new_lst, where for all x different from index, new_lst [ x] == lst [ x]

What am I missing?
How can we write lst[index] = lst[index]*2? How does the computer know that in this line of code, ‘lst[index]’ means two different things to the left and to the right of the equals sign??? (i.e., on the left it means ‘the element at index in the original list’, while on the right it means ‘the element at index in the output of this function’)

Ok, I just want to chime in and say that I got tripped up by the wording of the challenge, not the code!
I got the correct value but I misinterpreted the instruction:

The function should double the value of the element at `index` of `lst` and return the new list with the doubled value.

If `index` is not a valid index, the function should return the original list.

… I interpreted it to mean create a new list and put the doubled value of the element at index of lst in it.

If the instructions said " …double the value of the element at `index` of `lst` and return lst with the doubled value." I wouldn’t have got it messed up! I couldn’t understand why I was getting a red x when I was getting the correct doubled value!

The key here is the word return. The question is not ambiguous, though I’m not sure it is particularly edifying regarding functions which change lists. The assignment asks you to:

return the new list with the doubled value

It will accept any function which returns a list, be it the original list, a copy thereof, or an entirely new list. It will not accept a function which mutates the list in place, but offers no return value.

I’m confused as to why I am getting a message that says “Double the value at positions index in lst” when my code is giving the correct answer.

This is what I have written:

def double_index(lst,index):
if (index >= 0) and (index < len(lst)):
new_index = lst[index] * 2
return(new_index)
else:
return(lst)

This returns the correct answer to the sample problem.

The function should double the value of the element at `index` of `lst` and return the new list with the doubled value.

If `index` is not a valid index, the function should return the original list.

Is your function indented like this? (You can actually present your function as Python code, which includes indentations as a necessary part of its syntax, by using the </> icon that is in the middle of the menu bar at the top of the text box you are typing in.)

``````def double_index(lst,index):
if (index >= 0) and (index < len(lst)):
new_index = lst[index] * 2
return(new_index)
else:
return(lst)

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

``````

Output:

``````-20   # This is the (correctly) doubled value at index 2
[3, 8, -10, 12]   # This should be [3, 8, 20, 12]
``````

Srry but the text was very confusing :

<<The function should double the value of the element at `index` of `lst` and return the new list with the doubled value.

If `index` is not a valid index(??? a valid index???), the function should return the original list.>>

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.