Why Will One Loop Modify A List Of Lists, But The Other Won't
Solution 1:
This statement:
single_list = ...
assigns a value to the local variable named single_list
. It does not have any effect on any other data.
The statement:
single_list[i] = ...
modifies the value of the object referenced by the local variable named single_list
, specifically it sets the value of a particular element of the list.
Solution 2:
for list in myLists:
list = map(numParser, rawData)
The loop assigns "list" as a variable which references the list in myLists. when you reassign the "list" variable it now points to a new list, but the list in used to reference in "myLists" is unchanged. (Also, you shouldn't use "list" as a variable name as it is a Python keyword).
In the second example you do overwrite the reference to the list in myLists, so it alters myLists.
Solution 3:
In your first example you are assigning a new list to the name "single_list", in the second example you are modifying the list referenced by single_list.
Post a Comment for "Why Will One Loop Modify A List Of Lists, But The Other Won't"