Hello, this is for question #5 in https://www.codecademy.com/paths/data-science/tracks/dscp-python-fundamentals/modules/dscp-python-lists/articles/advanced-python-code-challenges-lists
#Write your function here
def double_index(lst, index):
if index > len(lst):
return "Index is out of range"
else:
new_index_number = lst[index] * 2
lst[index] = new_index_number
return lst
#Output gives me the correct answer, doubling the value in index of two. [3, 8, -20, 12]
#Uncomment the line below when your function is done
print(double_index([3, 8, -10, 12], 2))
///////////////////////
But here is the solution provided:
#Write your function here
def double_index(lst, index):
# Checks to see if index is too big
if index >= len(lst):
return lst
else:
# Gets the original list up to index
new_lst = lst[0:index]
# Adds double the value at index to the new list
new_lst.append(lst[index]*2)
# Adds the rest of the original list
new_lst = new_lst + lst[index+1:]
return new_lst
#Uncomment the line below when your function is done
print(double_index([3, 8, -10, 12], 2))
Why is it better than mine? Or is it?