 # Advanced Python Code Challenges: Lists - Question 4: Double Index

I’m not sure what I’m going wrong with my code for question four (Double Index). Seems to output the correct answer every time I’ve tested it. Why am I getting the " Make sure to define what should happen if `index` is too big!"?

The question is as follows:

``````Create a function named double_index that has two parameters: a list named lst and a single number named index.

The function should return a new list where all elements are the same as in lst except for the element at index. The element at index should be double the value of the element at index of the original lst.

If index is not a valid index, the function should return the original list.

For example, the following code should return [1,2,6,4] because the element at index 2 has been doubled:

double_index([1, 2, 3, 4], 2)
After writing your function, un-comment the call to the function that we’ve provided for you to test your results.
``````

Looking into this question I’ve found this one similar. Yet, pretty sure I’m following the question by creating a new list.

My code:

``````def double_index(lst, index):
#Saves the index integer to a variable to be doubled later
lst_index = lst[index]
#Doubles the lst_index while turning it back into a list so it can be concatinated.
double_index = [lst_index * 2]
#Gets the before and after parts of the origianl list
before_index = lst[:index]
after_index = lst[index + 1 :]
#Makes the new list
new_double_index_list = before_index + double_index + after_index
#The area I'm messing up in somehow. I've done greater than or equal to along with greater than which makes more sense to me. Neither work.
if index >= len(lst):
return lst
else:
return new_double_index_list
``````

Edits:
#1 - Spelling
#2 - Added notes into code so it’s easier to follow.

Hello @inexploratus!

Remember that list indices start at `0` and end at `len(my_list) - 1`. Looking at your code, what issue might come up if `index` is set to `len(lst`?

Hello @dr_victoria!

Thank you for taking the time to reply!

Took me about 15 minutes of muttering to myself and I’m still not sure how I’ve corrected the issue by moving the variables under the scope of else.

Code:

``````def double_index(lst, index):

#Testing dr_vic question
#print(len(lst))
if index >= len(lst):
return lst
else:

lst_index = lst[index]
double_index = [lst_index * 2]
before_index = lst[:index]
after_index = lst[index + 1 :]
new_double_index_list = before_index + double_index + after_index
return new_double_index_list
``````

Was thinking about the question you asked me, wouldn’t index set to len be fine with-in what this code is suppose to accomplish? As if you had 4 elements in lst, the max index would be 3. So inputting 4 as a possible index should return the origanal lst? Sorry if I’m missing the point!

Oh, I’m so sorry, I misread `>=` as `>` in your if statement. The issue you are having might actually be that you have to uncomment the provided call to the function in order to pass the test. You’ve also got a space before the colon in `after_index = lst[index + 1 :]`. I’m not sure if this will have any effect on the code, but it might help to remove that space. Hope this helps!

P.S. I’ve tested your code on the webpage and it passes the test.

@dr_victoria

Definitely will come back and try this question again once my understanding improves. Thank you!

Because the `if` occurs at the end. If the provided index is too large, you’ll get an error saying:

``````Index out of range
``````

Due to this code:

2 Likes

Hey so I’m just wondering as when i checked my code was way different and shorter then what they supplied as the solution, is there anything wrong with this code or is there a reason i should or shouldnt use this instead?

def double_index(lst, index):

try:

``````lst2 = lst[0:index]

lst2.append(lst[index]*2)

return lst2 + lst[index+1:]
``````

except IndexError:

``````return lst
``````

#Uncomment the line below when your function is done

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

I’m not sure if there’s a codecademy based solution for this one but I quite like explicit uage when index based list slicing is used (the `slice` function is good for this too) since I find it easier to follow the thought process and therefore the code itself but that is somewhat opinion based. So long as it is understandable without too much effort which I think it is anyway. Occassionally it makes me twitch but error handling for logic flow is considered acceptable in Python and you’ll find it used often enough.

Not sure I fully follow you, I did use slicing? Either way though im seriously new to this only my maybe 5th day practicing so i dont understand that much but atleast i caught it is acceptable and used fairly often for logic flow which i understand which is good lol because to me that just seemed a much easier and straightforward solution then the one I was proposed. But I assume thats because they’re trying to teach certain functions/techniques here and i backtracked into using the solution i felt was more logical from earlier on in the lesson. Anyways I just wanted to know if it was acceptable and i got my answer so thanks (y) Don’t want to instill bad habits so early on in my training haha.

Regarding slicing I was noting that sometimes making it clear what your slice indexing is makes it much easier for people to understand your code. Very basic example-

``````lst = ['Tom', 'Year 12', 13002, 'Saxaphone', 'Gymnastics']
school_details = lst[:3]  # Making it clear exactly what this slice relates to
name = lst  # Makes it clear what lst is
extracurricular = slice(3,5)  # an even less common slice method
print(lst[extracurricular])  # I do now know what is being printed though...
``````

It doesn’t come up that often since there are better data types for that sort of thing but you may have to use it occasionally. Worth keeping it mind just in case. For the given example I think it’s easy enough to understand the code without for the most part.

Something similar happens to me except that my code looks like this:

``````def double_index(lst,index):
if(len(lst) >= index):
lst[index] = lst[index] * 2
return lst
else:
return lst
``````

1 Like

Hello @sagodi97 and welcome to the Codecademy Forums!

Remember than the last index of a list is always `len(lst) - 1`.

What would happen if we called your function like so?

``````my_list = [3, 5, 4, 9, 7]

double_index(my_list, 5)
``````
2 Likes