Another related question is "How do I pass a variable by reference?".
Daren Thomas used assignment to explain how variable passing works in Python. For the append method, we could think in a similar way. Say you're appending a list "list_of_values" to a list "list_of_variables",
list_of_variables = [] list_of_values = [1, 2, 3] list_of_variables.append(list_of_values) print "List of variables after 1st append: ", list_of_variables list_of_values.append(10) list_of_variables.append(list_of_values) print "List of variables after 2nd append: ", list_of_variables
The appending operation can be thought as:
list_of_variables[0] = list_of_values --> [1, 2, 3] list_of_values --> [1, 2, 3, 10] list_of_variables[1] = list_of_values --> [1, 2, 3, 10]
Because the first and second item in "list_of_variables" are pointing to the same object in memory, the output from above is:
List of variables after 1st append: [[1, 2, 3]] List of variables after 2nd append: [[1, 2, 3, 10], [1, 2, 3, 10]]
On the other hand, if "list_of_values" is a variable, the behavior will be different.
list_of_variables = [] variable = 3 list_of_variables.append(variable) print "List of variables after 1st append: ", list_of_variables variable = 10 list_of_variables.append(variable) print "List of variables after 2nd append: ", list_of_variables
The appending operation now is equivalent to:
list_of_variables[0] = variable --> 3 variable --> 4 list_of_variables[1] = variable --> 4
And the output is:
List of variables after 1st append: [3] List of variables after 2nd append: [3, 10]
The difference between variable and list_of_values is the latter one changes in-place.
conditionandm? What isfi? What isf()?f1andfiis that a typo in your code?