0

So, here is my snippet. Here when I pass my original list (temp) to add_more. temp get updated.

temp = [1, 2, 3]
def add_more(l):
    l.append(5)
add_more(temp)
print(temp)

But here when I do this:

temp = [1, 2, 3]
def add_more(l):
    l = []
add_more(temp)
print(temp)

but here temp remain same as [1, 2, 3] and is not an empty list why?

Gaurav
  • 39
  • 2
  • Because the parameter ```l``` is over written by ```l =[]``` assignment –  Aug 28 '21 at 05:23
  • In the first case the list ‘l’ and temp are referring to the same list object where as in the second case, ‘l’ is a new list object that you created inside the function( temp and ‘l’ are not referring to the same list object). – Priya Aug 28 '21 at 05:24
  • @KetZoomer The snippet work perfectly fine. What are you not able to reproduce? – Gaurav Aug 28 '21 at 05:24
  • 1
    In the first example you are **changing** the object referenced by `l` and `temp`, in the second example you are **replacing** the object `l` references to. `temp` is not touched. – Klaus D. Aug 28 '21 at 05:27

5 Answers5

0

In the first example, you manipulate the list you have passed to the function. but, in the second one you assign an empty list to the label "l", as a result, the list you have passed to the function, does not change.

0

Because append is a mutable operation, and when the code inside the function l.append(5) is executed, it mutates the list l, in second case, when you are l=[] that's creation of an empty list, not the modification of the list passed.

Moreover, you can use id to verify it

First approach

temp = [1, 2, 3]
def add_more(l):
    print(f'id of l:{id(l)} before')
    l.append(5)
    print(f'id of l:{id(l)} after')
add_more(temp)
print(f'id of temp: {id(temp)}')

id of l:1605604079816 before
id of l:1605604079816 after
id of temp: 1605604079816

Second approach

temp = [1, 2, 3]
def add_more(l):
    print(f'id of l:{id(l)} before')
    l = []
    print(f'id of l:{id(l)} after')
add_more(temp)
print(f'id of temp: {id(temp)}')

id of l:1605677713736 before
id of l:1605604079816 after
id of temp: 1605677713736

In the first approach id, that is essentially the memory representation of a variable, is same for temp and before and after inside the function.

But in second case, id is same for temp and before in the function, but not after because you are just creating an empty list and assigning it to l, which is just a variable local inside the function.

ThePyGuy
  • 17,779
  • 5
  • 18
  • 45
0

In the first example, you have passed the reference of temp in the function. The .append method then adds the new element to it. append mutates the list and the reference of the list passed is mutated

In the second example, the parameter l is over written by the assignment expression l = []creating a variable l local to the function only. So temp remains unchanged.

0

This is related to How do I pass a variable by reference?

In short, list as a parameter is mutable. In your first snippet, l points to what temp points to, and thus modification of l is also performed for temp.

In your second snippet, l = [] points l to this new empty list, while temp still points to the original one.

Hope this answers your question!

0

l is not the list. It is a reference to that list.

If you want to change the value of the list in a function (rather than the value of the parameter that refers to the list), you must use slicing:

temp = [1, 2, 3]
def add_more(l):
    l[:] = []
add_more(temp)
print(temp)
#[]
DYZ
  • 55,249
  • 10
  • 64
  • 93