Hello!
I am doing Python Code Challenges: Lists (Advanced) 4th, given solution code was like this:
def double_index(my_list, index):
# Checks to see if index is too big
if index >= len(my_list):
return my_list
else:
# Gets the original list up to index
my_new_list = my_list[0:index]
# Adds double the value at index to the new list
my_new_list.append(my_list[index]*2)
# Adds the rest of the original list
my_new_list = my_new_list + my_list[index+1:]
return my_new_list
Mine solution was like this:
#Write your function here
def double_index(my_list, index):
if index >= len(my_list):
return my_list
else:
num = my_list[index] * 2
my_list.insert(index, num)
my_list.pop(index+1)
return my_list
#Uncomment the line below when your function is done
print(double_index([3, 8, -10, 12], 2))
Does anyone see any problem that may arise with my code? Or why should I do it like the given solution? It is my 6th day learning this, so I’m checking with experienced people.