What does index of lst even mean?

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.

Codecademy needs to add some more baby steps leading up to this.

1 Like

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

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! :crazy_face:

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