This question already has answers here:
Everything in Python is passed and assigned by value, in the same way that everything is passed and assigned by value in Java. Every value in Python is a reference (pointer) to an object. Objects cannot be values. Assignment always copies the value (which is a pointer); two such pointers can thus point to the same object. Objects are never copied unless you're doing something explicit to copy them.
For your case, every iteration of the loop assigns an element of the list into the variable
loc. You then assign something else to the variable
loc. All these values are pointers; you're assigning pointers; but you do not affect any objects in any way.
Is Python pass-by-reference or pass-by-value?, The two most widely known and easy to understand approaches to parameter passing amongst programming languages are pass-by-reference� Python uses a mechanism, which is known as " Call-by-Object ", sometimes also called " Call by Object Reference " or " Call by Sharing " If you pass immutable arguments like integers, strings or tuples to a function, the passing acts like Call-by-value. It's different, if we pass mutable arguments. Pass by reference vs value in Python, Is Python Argument Passing model a “Pass by Value” or “Pass by Reference”? Any operation performed by the function on the variable or the object will be� If Python were call by value, the function would’ve received a copy of the list, and wouldn’t have been able to change it. What happens is the function receives a reference to the object passed in, but as soon as you try to assign to the parameter (as we did in the first case), the binding between the parameter and the original object that was passed is broken, and a new local variable is created.
It doesn't help in Python to think in terms of references or values. Neither is correct.
In Python, variables are just names. In your for loop,
loc is just a name that points to the current element in the list. Doing
loc =  simply
rebinds the name
loc to a different list, leaving the original version alone.
But since in your example, each element is a list, you could actually
mutate that element, and that would be reflected in the original list: for loc in locs:
loc = loc * 2
Python : When is a variable passed by reference and when by value , Everything in Python is passed and assigned by value, in the same way that everything is passed and assigned by value in Java. Every value� First thing first, according to official Python documentation, Python neither passes by object nor by reference. Python passes by ‘ Assignment ‘. What it basically means is that, the parameter is actually reference to the object. In other words variables are nothing but just names assigned to or bound to an object.
When you say
loc = 
you are rebinding the
loc variable to a newly created empty list
Perhaps you want
loc[:] = 
Which assigns a slice (which happens to be the whole list) of loc to the empty list
Python2 Tutorial: Passing Arguments, As a consequence, the function can modify the argument, i.e. the value of the variable in the caller's scope can be changed. The advantage of call-by- reference� By reference vs by value is definitely one of them. I remember back in the day when I read up on the topic and every source I went through seemed to contradict the previous one. It took some time
Everything is passed by object. Rebinding and mutating are different operations.
locs = [ ,  ]
for loc in locs:
Pass by value vs. pass by reference, Pass by value means that a copy of the actual parameter's value is made in memory, i.e. the caller and callee have two independent variables with the same � Just a terminology note: Python already uses pass-by-value. Many values just happen to be references, so you're passing references by value. If Python used pass-by-reference then def f(x): x = 5 would actually change the value of the expression the caller was passing for the caller, but it doesn't. – Laurence Gonsalves May 10 '09 at 17:31 Are arguments passed by value or by reference in Python?, The parameter passed in is actually a reference to an object, as opposed to reference to a fixed memory location but the reference is passed by value. In addition,� I have the following python code which sets the root of an AVL tree to a specified value. But it seems that setting the root variable of the class has no effect when done by passing it through a fu Pass by reference vs. value (Pass by Object): IST Advanced Topics , When a parameter is passed by reference, the caller and the callee use the same Python uses a mechanism, which is known as "Call-by-Object", sometimes� In call by reference the actual value that is passed as argument is changed after performing some operation on it. When call by reference is used, it creates a copy of the reference of that variable into the stack section in memory. Is uses a reference to get the value.
Function pass by value vs. pass by reference, Critical to the discussion here is that this memory holds the formal parameter values and function local variables. By definition, pass by value means you are� Tag: python,list,dictionary. It is possible to map a dictionary key to a value that is a reference to a mutable object, such as a list. Such a list object can be changed by invoking a list method on the reference, and the changes will be reflected in the dictionary. This is discussed in: Python: How do I pass a variable by reference? and
Comments Everything is passed by value, but every value is just a reference ;) In my point of view - You described it the best. Just tell me the answer of the Update to the question - that part you missed. So what happens with integer and float numbers then? Are they just considered immutable objects? How does python manage that as I'm sure it cannot hold all conceivable numbers in memory ready to be referenced. @AndrewS: Integers and floats are indeed immutable (it should be apparent that they do not have methods that mutate themselves). I am not sure what the second part of your comment means. What I'm saying by that second point is lets sat that I have two lines X = 150 and y = 150 on another. Will there be two separate copies of 150 in memory or one immutable object pooled in the same way Java Pools it's immutable Strings. @AndrewS: Maybe they are the same and maybe they aren't. Why does it matter? This has nothing to do with mutability or semantics of assignment. It might be helpful to read Idiomatic Python - "other languages have variables, Python has labels." The rest of it is good reading too. ;) How to modify each element of the container ? \ @Yugal which part of what I've written above was not clear? Generally, I just want to process each element of my container. So, how can that be acheived ? If everything is a label that seems to be a disadvantage when actaully want to edit the element. @YugalJindle : To modify elements in a list, use the square brackets to reference items in that list. I believe this last example is what OP was looking for !