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

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.

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.)

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.

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?

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.