FAQ: Code Challenge: Lists - Double Index


#1

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

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


#3

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
else:
return lst.append(new_index)


#4

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.


#5

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.


#6

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


#7

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.

#8

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


#9

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


#10

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:


#11

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
else:
return lst