FAQ: Code Challenge: Lists - Double Index


This community-built FAQ covers the “Double Index” exercise from the lesson “Code Challenge: Lists”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science
Data Science

FAQs on the exercise Double Index

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!


2 posts were split to a new topic: What does index of lst even mean?


I’m using this code, but it keeps saying that I didn’t defined what to do if “index is too big”

def double_index (lst, index):
new_index = lst[index]*2
lst_len = len(lst)

if index > lst_len:
return lst
return lst.append(new_index)


Nowhere in the previous lesson did I learn that a specific index within a list could be assigned a new value like any other variable through lst(index) = 2 * lst(index). The error message and hint did not give me any clarity as to this fact. I really would’ve liked to solve this without clicking on the solution.


if index > lst_len will still allow for one index higher than the list itself contains; remember, they’re zero-indexed! Replacing > with >= should solve that issue.


Hello coders,

I have tried many solutions on this exercise…my brain is a bit fried right now…this was my last solution and I keep getting the Codecademy message at the bottom “Make sure to define what should happen if INDEX is too big.” I cannot get around this message and I really would like to solve this function without getting solution…any help would be greatly appreciated:)

def double_index(lst, index):
new_list = lst[index] * 2
if index < len(lst):
return new_list
elif index > len(lst):
return lst

print(double_index([3, 8, -10, 12], 2))


The general approach is to the validating before attempting to run code on the object. If we run the code first, we may hit upon an index out of range exception before the validation line is reached.

if len(lst) >= index:
    lst[index] *= 2
return lst

In both cases the list is returned, but is only modified when the index is valid.

From what I recall, early on in the unit on lists we are shown that lists are mutable, meaning we can change the values, add new elements and remove elements.

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 took the last instruction to mean don’t do anything to the list, just return it. There is no expected message, just the original list.

if index is valid (that is, within range)
then change the element at that index
else do nothing
in both cases return the list.


#Write your function here

def double_index(lst, index):
new_index = lst[0] * 2
new_lst = lst.append(new_index)
if len(lst) >= index:
return lst
return new_lst

I tried this but I have always the same error message "double the value at the position index in lst "…of course because append add only at the end of the lst. I just have to return lst with the new_index at the right position (index) but how I can do that? is there any function? like append?

Another question, is there any debugger in code academy to see step by step what the program is doing?


We don’t want a new index, but a new value at index position in the list. The above is fixed upon the first position which you are doubling and appending, thereby changing the length of the list. It does not do what the instructions tell us to do.

Remove these two lines…

new_index = lst[0] * 2
new_lst = lst.append(new_index)

This line is correct,

if len(lst) >= index:

but it is also the case where we set the new value at that index…

    lst[index] *= 2

The list will only be changed by that line inside the IF block. Now you have only to return the list, after the if statement. No special function required.

Edit: fixed typo in example


Thank you very much for your answer. I realized that I it was not clear for me the purpose of the exercise.
I went in another direction…
I am gonna try the new one :slight_smile:


I really dont understand why this doesnt work, can someone help?

def double_index(lst, index):
if index < len(lst):
new_list = lst[index] * 2
return new_list
return lst


Because you are not modifying and returning the original list. Once you have validated the input (index) and have found it to be in range, then what is left to do is modify the element at that index, in place.

lst[index] *= 2

Now just return the original list.


Thank you for the hint, almost forgot the function of “*=”


Here is page to bookmark…



ohhhhh thank you so much i could not get my head around this


Nice one took me a while to finish…

i came up with :

#Write your function here
def double_index(lst,index):
  if index >= len(lst):
    return lst
    lst[index] = lst[index] * 2 
    return lst

print(double_index([3, 8, -10, 12], 2))

Fails with an index of -5 but i think that’s out of scope, the solution passes the test.

If i click the Solution, the provided solution seems to fail!?


Seems to?


my solution:
def double_index(lst, index):
lst[index] *= 2
return lst
except IndexError:
return lst


The instructions seemed a bit confusing, especially when it said " The function should double the value of the element at index of lst" I wasn’t sure if it meant to double the index or lst.

I also don’t recall learning about using a variable to locate an item in a list (lst[index]) and I think the code that was written already to test our answer should have had a comment describing what result I should have received.

This challenge just seemed a bit advanced for what I’ve learned so far about Python.


3 posts were split to a new topic: Spoiler