FAQ: Code Challenge: Lists - Double Index

I’m having trouble understanding line of the solution:

lst(index) = 2*lst(index)

How is this not equivalent to redefining the lst variable itself?

Could i write another function that says the following?

def double_lst(lst):
  	lst = lst* 2
  return lst
  1. Use[] , not ().

  2. It’s the same as:

x = 5
x = 2 * x
print(x)

# Output:
10

Here, x is lst[index]. That’s the point of this exercise: lst[index] is the element of lst at the given index. Using index notation gives you direct access to that element.

  1. You could write such a function, but as written, it won’t do what you want. Why don’t you try it out, to see if it works? It’s easy enough to do so, and you might be surprised. ( Some modules, such as NumPy give you similar functionality to what you want, though.)

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.

Then they shouldn’t be giving us a challenge on material that was never covered in the previous lesson.
I don’t understand why the curriculum people thought this was a good idea? And do they even read the wording on these problems?
I seriously went back through the lessons thinking I missed material.
The sheer incompetence on this challenge appalled me.