Would this be a valid code for this exercise?
def double_index(lst, index):
if index <= len(lst) - 1:
lst[index] *= 2
return lst
else:
return lst
#Uncomment the line below when your function is done
print(double_index([3, 8, -10, 12], 2))
The challenge is,
The function should return a new list where all elements are the same as in lst
except for the element at index
, which should be double the value of the element at index
of lst
.
Does your function return a new list, or change the original list?
my_lst = [3, 8, -10, 12]
idx = 2
print(my_lst)
print(double_index(my_lst, idx))
print(my_lst)
Output:
[3, 8, -10, 12]
[3, 8, -20, 12]
[3, 8, -20, 12] # the list has changed