If I do this

mylist = [1,2,3,4]
a = mylist
a[0] = 'hello'

>>>mylist

output : 'hello',2,3,4

I find that the value of mylist has changed to
'hello',2,3,4

So my question is..
If I assign one variable as a value of another (like saying a is mylist), then is 'a' another "copy" of mylist or does it simply point to the original variable "mylist"? From the output I got, it seems that way.. but I want to confirm.

Recommended Answers

All 13 Replies

It points to the original variable.
This is true for all variables containing objects.
For a list, if you want to duplicate it, you have to do

mylist = [1,2,3,4]
    a = mylist[:]
    a[0] = 'hello'
    print mylist

Just a reminder, for nested lists a plain copy like a = mylist[:] or a = list(mylist) won't work ...

mylist = [1,2,[3,4]]
a = mylist[:]
a[2][0] = 'nest'
print mylist  # [1, 2, ['nest', 4]]  ouch!!!
print a       # [1, 2, ['nest', 4]]

Here you have to apply module copy ...

import copy
mylist = [1,2,[3,4]]
a = copy.deepcopy(mylist)
a[2][0] = 'nest'
print mylist  # [1, 2, [3, 4]]
print a       # [1, 2, ['nest', 4]]

This is true for all variables containing objects.

What does "variables containing objects" mean?

What does "variables containing objects" mean?

I think he means everything but basic datatypes like int, floats and strings. However, you can do as if it was always true.

ah... but why the difference? why do basic data types exhibit one behavior and things like list another?
(I suppose it is more intuitive.. I just wrote my last program without any knowledge of this and it still works.. lucky me ;-))

Python has a couple of container objects whose contents can be easily changed, mainly lists and dictionaries. These are called mutable objects. It would be unwieldy to create a new list every time you changed an item inside the list. So mutable objects have special memory considerations that you have to heed. One of those considerations is mutable object copy.

ah... but why the difference? why do basic data types exhibit one behavior and things like list another?
(I suppose it is more intuitive.. I just wrote my last program without any knowledge of this and it still works.. lucky me ;-))

In fact I think it's true for all datatypes as the following experiment shows

>>> myInt = 3764
>>> a = myInt
>>> id(myInt)
22352896
>>> id(a)
22352896

myInt and a have the same id, meaning that they refer to the same object.

yeah.. but unlike a list, changing the value of a will not affect the value of my int.

I think he means everything but basic datatypes like int, floats and strings. However, you can do as if it was always true.

This is what i meant.
Try this and think of what it means :

a=13
b=a
b=14
print a
a=[1,2,3]
b=a
b[1]=4
print a
a=[1,2,3]
b=a
b=[4,5,6]
print a

Simply think that, when you assign a variable to another, it is a reference assignment. If you ch
just think b becomes another variable and does not reference a anymore.

I typed the wrong key...
So... Simply think that, when you assign a variable to another, it is a reference assignment. If you change PART of b, you'll change a too. But, if you reassign b, just think b becomes another variable and does not reference a anymore.

Sorry, I typed the wrong key... So...
Simply think that, when you assign a variable to another (say b=a), it is a reference assignment. If you change PART of b, you'll change a too. If you reassign b, just think it becomes another variable and does not reference a anymore so a and b will be different.

ah.. thanks. That clears up a lot. Is this behavior a quirk or is python made that way?
PS: I think all forums should automatically add a post to say that there is another page in a thread... what just happened to you happens all the time to me too...

As far as I know, this is some kind of standard behaviour.
Old languages [ troll] like C [ /troll] made difference between value and reference...
To have this behaviour in "non object" langage (in C), you passed pointers for "reference" assigment.
In object languages, especially when you don't have pointers (maybe vegaseat could lighten un about C++) reference is the standard way.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.