Help Requested - 13. Modifying Lists


This code works, but I'm confused about one aspect: why can't we write "for i in x" in place of "for i in range(0, len(x))?"

n = [3, 5, 7]

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


because writing for i in x will give you read only values from the list, range() produces a list of numbers which if done right, match the indexes of your list

you need this index values to update a list element/value


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