Modify list items


#1



https://www.codecademy.com/courses/python-beginner-nzzVa/3/4?curriculum_id=4f89dab3d788890003000096

Hi, just wondering why the values in the list could not be doubled in this way, although I know another way of calling index could work.


a=[1,2,3]
for item in a:
    item=item*2
print a


#3

item is a local variable only existing inside the for loop, it gets re-assigned for the next run of the loop, and it cease to exist after the loop is finished. So your updates aren't stored anywhere, which is why it isn't working


#4

So can I understand it like this. "for x in lists", the system created a local variable name "x",each time "x" is assigned to different value in lists. "x" is a copy of the value in lists, not a part of lists itself.


#5

I would put it the other way around: x gets assigned a (copy of) value of the list.

Yep, this is absolutely right. Where as x would be a index, you use it to access the list, and then update a value in the list. You seem to have understand in perfectly


#6

thanks stetim, absolutely helpful!