What does index of lst even mean?

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 :slight_smile:

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
1 Like

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.

2 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?