i am trying to do this
consider

a=[[(34,5),(34,6)],[(35,5),(35,6)]]
b=[(36,5),(36,6)]

i want to put elements of b in a so i do

a.append(b)

now if i do

b.pop()

why does it effect a?

a ends up being `a=[[(34,5),(34,6)],[(35,5),(35,6)],[(36,5)]]` WHY??

is there a way around it??

i don't think i had this trouble in C++

Because you've appended a pointer to that list, so when b changes the pointer sees the change. However if you make a COPY it has its own list. Consider the following interactive experiment:

>>> f=['bob','jack']
>>> d=['bill']
>>> d.append(f)
>>> d
['bill', ['bob', 'jack']]
>>> f.pop()
'jack'
>>> f
['bob']
>>> d
['bill', ['bob']]
>>> f.append('jack')
>>> f
['bob', 'jack']
>>> g=f[:]
>>> g
['bob', 'jack']
>>> f.pop()
'jack'
>>> f
['bob']
>>> g
['bob', 'jack']

and of course:

>>> d.append(g)
>>> d
['bill', ['bob', 'jack']]
>>> f
['bob']
>>> f.append('cow')
>>> f
['bob', 'cow']
>>> g
['bob', 'jack']
>>> d
['bill', ['bob', 'jack']]

Edited 5 Years Ago by pyguy62: n/a

a = [[1,2,3]]

a here refers to the list object [[1,2,3]].

b = [4,5,6]

b here refers to the list object [4,5,6]

a.append(b)

a is now [original_list_object_a,original_list_object_b].
In other words, a = [[1,2,3], b]
So, when you change b, you change what a refers to.

You can verify this through id -

>>> a = [1,2,3]
>>> a = [[1,2,3]]
>>> b = [4,5,6]
>>> a.append(b)
>>> id(a)
35723400L
>>> id(a[1])
35724424L
>>> id(b)
35724424L

Notice how a[1] and b have the same ID. That's because they point to the same object
If you want to create a copy of b, and therefore point to a different object, you can use

a = [[1,2,3]]
b = [4,5,6]
a.append(b[:])

Or use the copy module.

Check out the difference between copy and deepcopy as well. You probably want deepcopy (not a copy, and not a reference pointer to the list) since b is a list of tuples

a=[[(34,5),(34,6)],[(35,5),(35,6)]]
b=[(36,5),(36,6)]
a.append(copy.deepcopy(b))

Edited 5 Years Ago by woooee: n/a

Comments
good advice, didn't even think of it.

thanks a lot guys i totally got it .....kinda a new to python ( more of C++ person) dint get the implicit use of pointors in the function.

I feel like it's more a matter of it being explicit copying than it being implicit pointers, review the zen of python. Don't forget to mark the thread solved etc. Happy we could help.

Edited 5 Years Ago by pyguy62: n/a

thanks a lot guys i totally got it .....kinda a new to python ( more of C++ person) dint get the implicit use of pointors in the function.

Just a note, but they don't work like C pointers.

In Python, we have objects and names.

Objects are -

Unique
A type of some kind
Not None
They can be referred to by names, or not at all by names.
They can have methods and data attached.

Names -
What we use to refer to objects.
Any amount of names can refer to the same object

For more information, read this thread from the edu-sig mailing list - http://mail.python.org/pipermail/edu-sig/2008-May/008529.html

This question has already been answered. Start a new discussion instead.