5

In Python, lists are passed by reference to functions, right?

If that is so, what's happening here?

>>> def f(a):
...     print(a)
...     a = a[:2]
...     print(a)
...
>>> b = [1,2,3]
>>> f(b)
[1, 2, 3]
[1, 2]
>>> print(b)
[1, 2, 3]
>>>
4
  • 3
    a[:] = a[:2] is what you should be doing. Jul 26, 2017 at 19:27
  • 2
    Or del a[2:] would work as well, and be more readable.
    – Artyer
    Jul 26, 2017 at 19:29
  • f receives a reference to the list, not to the b variable; assigning to a will not affect the b variable. While f is passed a reference, "pass by reference" has a specific meaning that does not apply to Python's parameter passing model. Jul 26, 2017 at 19:34
  • 1
    nedbatchelder.com/text/names.html is the best reference I've seen for learning about how this part of Python works. Jul 26, 2017 at 19:35

3 Answers 3

6

In the statement:

a = a[:2]

you are creating a new local (to f()) variable which you call using the same name as the input argument a.

That is, what you are doing is equivalent to:

def f(a):
    print(a)
    b = a[:2]
    print(b)

Instead, you should be changing a in place such as:

def f(a):
    print(a)
    a[:] = a[:2]
    print(a)
2
  • 1
    Nitpick: You are not creating a new local variable / overshadowing an input argument. Arguments are local variables. You are just reusing the same name.
    – Artyer
    Jul 26, 2017 at 19:35
  • @Artyer You are right. I was kind of careless with terminology here. Edit will follow. Thanks.
    – AGN Gazer
    Jul 26, 2017 at 19:37
5

When you do:

a = a[:2]

it reassigns a to a new value (The first two items of the list).

All Python arguments are passed by reference. You need to change the object that it is refered to, instead of making a refer to a new object.

a[2:] = []
# or
del a[2:]
# or
a[:] = a[:2]

Where the first and last assign to slices of the list, changing the list in-place (affecting its value), and the middle one also changes the value of the list, by deleting the rest of the elements.

3
  • array slices are not passed by reference. Dec 24, 2021 at 10:13
  • @DavidBandel If you are talking about something like f(a[:2]), that passes the result of evaluating a[:2] "by reference" to f. On lists, this creates a copy of some part of the list, but that is beside the point (that Python has no "pass-by-value" semantics). For example, f(x := a[:2]); x[0] is a[0] could be False for a list.
    – Artyer
    Dec 25, 2021 at 0:52
  • How can you even regard something as having been passed by reference if you are never again able to access the thing our "reference" is a pointer to? I'd regard it as a grey area in the value/reference dichotomy. Jan 3, 2022 at 14:22
4

Indeed the objects are passed by reference but a = a[:2] basically creates a new local variable that points to slice of the list.

To modify the list object in place you can assign it to its slice(slice assignment).

Consider a and b here equivalent to your global b and local a, here assigning a to new object doesn't affect b:

>>> a = b = [1, 2, 3]    
>>> a = a[:2]  # The identifier `a` now points to a new object, nothing changes for `b`.
>>> a, b
([1, 2], [1, 2, 3])
>>> id(a), id(b)
(4370921480, 4369473992)  # `a` now points to a different object

Slice assignment work as expected:

>>> a = b = [1, 2, 3]    
>>> a[:] = a[:2]  # Updates the object in-place, hence affects all references.
>>> a, b
([1, 2], [1, 2])
>>> id(a), id(b)
(4370940488, 4370940488)  # Both still point to the same object

Related: What is the difference between slice assignment that slices the whole list and direct assignment?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.