0

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++

4
Contributors
6
Replies
8
Views
5 Years
Discussion Span
Last Post by Enalicho
Featured Replies
  • 2

    [QUOTE=;][/QUOTE] Check out the difference between [url=http://docs.python.org/library/copy.html]copy and deepcopy as well[/url]. You probably want deepcopy (not a copy, and not a reference pointer to the list) since b is a list of tuples [code]a=[[(34,5),(34,6)],[(35,5),(35,6)]] b=[(36,5),(36,6)] a.append(copy.deepcopy(b))[/code] Read More

  • [QUOTE=asa88;1651028]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.[/QUOTE] Just a note, but they don't work like C pointers. In Python, we have objects and names. Objects are - Unique A … Read More

0

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 by pyguy62: n/a

0
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.

2

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 by woooee: n/a

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

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.

0

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 by pyguy62: n/a

1

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.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.