Coming from a MATLAB background and looking to get some clarification on the following behavior:
When looping on a list of lists, the looped variable seems to be affected within the loop:
foo = [['string1'],['string2'],['string3']]
for item in foo:
item.append('test')
print(foo)
[['string1', 'test'], ['string2', 'test'], ['string3', 'test']]
Why does foo
update when item
is being appended?
On a similar note, why do some methods need to be assigned an output and other don't? For example on lists, you can append by simply:
list.append(new_item)
But for strings, the output needs to be assigned:
string = string.replace('old','new')