Double Index Code Challenge question

Regarding this exercise:

Create a function named double_index that has two parameters named lst and index .

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.

(https://www.codecademy.com/paths/data-science/tracks/dspath-python-lists-and-loops/modules/dspath-lists/lessons/python-functions-lists-cc/exercises/double-index)

#Write your function here
def double_index(lst, index):
if index < len(lst):

   I DON'T UNDERSTAND THIS LINE IN THE SOLUTION:
   lst[index] = lst[index] * 2

return lst

#Uncomment the line below when your function is done
print(double_index([3, 8, -10, 12], 3))

HOW CAN SOMETHING BE SET TO EQUAL ITSELF TIMES 2? SEEMS LIKE THERE SHOULD BE A CLEARER WAY TO REPRESENT THIS? THANKS.

it can’t, but in programming a single equal sign (=) means assign. It doesn’t mean set equal to. So we get the value from lst at index, multiple it by 2, and store it in the list again.

Thanks for the reply. That helps.

You can also use this operator:

    lst[index] *= 2

I believe it accomplishes the same thing as: lst[index] = lst[index] * 2

1 Like