13/18 "for i in range (0, len(x)): "


In the following code, if I use “for i in x:” instead of "for i in range(len(x)): ", it returns error “IndexError: list index out of range”. Could anyone explain? Thanks!

n = [3, 5, 7]

def double_list(x):
for i in range (len(x)):
x[i] = x[i] * 2
return x

print double_list(n)


using for i in x will give you the values from the list directly. Values in a list might be higher then the highest index.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.