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

Dani AI

Generated

What you are seeing is aliasing: a holds a reference to the same list object as b, so mutating b mutates the sublist inside a. If you want a to gain a new sublist that is independent of b, create a new list at append time.

# add b as a new, independent sublist
a.append(list(b))          # works on Py2/3
# on Python 3.3+ you can also do: a.append(b.copy())

# quick check that they are different objects
assert a[-1] is not b

If instead you meant to "put the elements of b into a" (without creating another level of nesting), use a.extend(b). Note this changes the shape of a by adding tuples directly to the top level, so only use it if that structure is intended. The difference between append and extend is covered in the tutorial under More on Lists at docs.python.org.

A shallow copy is sufficient here because b is a flat list of tuples of ints (all immutable). Deep copying would be unnecessary overhead; reserve it for cases where inner elements are mutable (e.g., lists of lists). The Python FAQ discusses copying objects and when shallow vs. deep copies are appropriate: How do I copy an object in Python?.

Recommended Answers

All 6 Replies

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']]
Member Avatar for Member #912166
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))
commented: good advice, didn't even think of it. +3

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.

Member Avatar for Member #912166

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

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.