Not good validation in Double Index


Python List double index

I was looking at this solution:

#Write your function here
def double_index(lst, index):
  if index < len(lst):
  	lst[index] = lst[index] * 2
  return lst

#Uncomment the line below when your function is done
print(double_index([3, 8, 9, 12], -7))

i think IF statement is not correct validation. I can write negative numbers in the len of list. In this case -4. But if i put in index -5 i get error. It is smaller than len(lst) but it is not valid index.


This has come up in conversation but for this exercise, there are no negative values tested so the above positive validation is enough to pass the lesson.

You are correct though in that negative indices are just as valid as positive ones so long as they are in range.

n = len(lst)
if -n <= index < n:
   # index is in range


if index in range(-n, n):
    # index is in range


Ok but still. This is not good solution it is misleading. I would newer klik on solution but this one made me how did they do it.


It occurs to me that negative indices were introduced in the early going of the unit on lists, and the author could have taken advantage of that fact by reinforcing it with the exercise. I won’t presume to speak for the author, but it may have been a simple oversight.