Sorry for perhaps noob question. However, I would expect the values of ~list remain unchanged yet they change along with ~list1. And it seems to be the case only for .do. Is there any solution to this or do I need to assign the original values to the ~list once again after I execute the function ?
Not exactly. For List, Set, Dictionary, etc, it adds to the same object and returns the same object.
For arrays, it will return the same object unless it’s already used up all of its slots, and then it will return a new one. Because you can’t be 100% sure of sticking with the same object, you have to assume the worst and always write array = array.add(x);.