Title: Wierd Behavior Appending to list inside function
Tags: python;list;append
Question: ```def test(list2):
    list2.append(1)
    print len(list2)
    print len(LIST1)

LIST1 = [1]

while len(LIST1) < 9:
    test(LIST1)
```

Please explain why 'LIST1' is increasing in size if I'm appending to 'list2', isn't variables inside functions local? And above all, how can I circumvent this?

The same happens if I make a new variable:

```def test(arg_list):
    list2 = arg_list
    list2.append(1)
    print len(list2)
    print len(LIST1)

LIST1 = [1]

while len(LIST1) < 9:
    test(LIST1)
```

Comment: In your first example, `my_list` is not defined, and neither `list1` nor `list2` are modified, and `LIST2` is not defined.

Comment: Sorry, bad formatting. Fixed.

Here is the accepted answer: No, parameters passed to a function are by reference, and in the second example the local variable is yet another reference to the same list. Parameter passing and variable assignment do not create copies of the list, only references to the same object. In other words: anything you do to an object being referenced inside the function (say, a list) will be reflected on the object itself once the function exits - the function parameter and the object "outside" the function are one and the same.

How can you circumvent this? well, if it fits your usage scenario you can simply copy the list before passing it as a parameter, like this:

```test(LIST1[:])
```

The above will create a new, different list (it's a shallow copy, though ... for performing a deep copy use ```copy.deepcopy```), which you can safely modify inside the function, as the list "outside" the function will remain unchanged.

Comment for this answer: Thanks. Spot on. Btw what's the difference between shallow copy and deepcopy?
Comment for this answer: So if I modify a element in a shallow copy of a list, it alter the element in original list?
Comment for this answer: @iCodez Got it, I added that to my answer. Thanks!
Comment for this answer: @f.rodrigues In a shallow copy, the list is new but the elements are the same. In a deep copy, the list is new and its elements are also copied, recursively.
Comment for this answer: @f.rodrigues if it's a mutable object (say, another list or a map), yes indeed!. An immutable object (say, a string or a number) can't be modified in-place, so it's not a problem


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:11/12/14', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<python><list><append>', 'Title': 'Wierd Behavior Appending to list inside function', 'ViewCount': 530, 'id': 26894621, 'user_AboutMe': '<p>Art major and hobbyist programmer. Interested in Game-Development and Image-Manipulation(Moving and Still).</p>\n\n<h1>SOreadytohelp</h1>\n', 'user_AccountId': 3614746.0, 'user_DisplayName': 'f.rodrigues', 'user_Reputation': 3393.0, 'user_Views': 532.0, 'user_id': 3014930.0}}