Please help me understand: Why does my code omit “8” (lst[1])?
print(double_index([3,8,-10,12]) prints [3,-20,12] to the screen instead of [3,8-20,12]. I don’t fully understand why. Can someone explain this to me please?
Here is my code:
#Write your function here
def double_index(lst, index):
list_until_index = lst[:index]
print(list_until_index)
list_after_index = lst[-index+1:]
print(list_after_index)
number_in_question = lst[index]
double_number_in_question = 2 * number_in_question
new_list = list(zip(list_until_index, [double_number_in_question], list_after_index))
if len(lst) > index:
return new_list
else:
return lst
#Uncomment the line below when your function is done
print(double_index([3, 8, -10, 12], 2))
Hello @code3928442758. Welcome to the forum. You’ve created a very complicated function to complete a fairly simple task. No need for slicing and zipping. You can simply change the value of the list element at the index requested, and return the list.