Consider the following code: -
listA = [['a', 'b', 'c', 'd'], ['e', 'f', 'g', 'h'], ['i', 'j', 'k', 'l']]
-
changeCoordinates = ((2, 3), (1, 3))
-
-
for coordinateList in changeCoordinates:
-
listB = listA[:]
-
x = coordinateList[0]
-
y = coordinateList[1]
-
-
listB[x][y] = 'foo'
-
print "list A: %s" % listA
-
print "list B: %s" % listB
-
listA should never change. But it does. It works the way I expected on a one-dimensional list, but on a two-dimensional list, listA changes. How do I avoid this, and (out of curiosity) why the hell doesn't python just assign the value rather than the reference anyway?
3 2045 boxfish 469
Recognized Expert Contributor
Hi,
listB is actually a separate copy of listA, but each of the sub-lists in listB is just a shallow copy of the corresponding sub-list in listA, if that makes any sense. To make a completely independent copy of listA, you will either have to use a loop like this: - for sub_list in listA:
-
listB.append(sublist[:])
or use the deepcopy() function of the copy module: - import copy
-
listB = copy.deepcopy(listA)
Hope this helps.
OK thanks. And about the second part of my question...is there any logical reason for python to do this? Isn't it inconsistent for the assignment operator to assign a value to some data types and a reference to others? Just wondering why they did this.
boxfish 469
Recognized Expert Contributor
Not sure. Seems like the only types that get copied as a reference are lists and object types. I think it would make more sense if tuples, strings, and dictionaries got copied as references too, because they can get really huge also. I wonder if there's a way to do shallow copying of those types in Python.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Andreas Kuntzagk |
last post by:
Hi,
There are three ways to (shallow)copy a list l I'm aware of:
>>> l2=list(l)
>>> l2=l
>>> l2.copy.copy(l)
Are there any differences? Are there more (reasonable) ways?
I think the first...
|
by: Eric Lilja |
last post by:
Hello!
Consider the following complete program (with sample output). There's
something wrong with my call to std::copy() in the copy constructor of
the Unit class. It fails to copy the list. I...
|
by: Martin Ortiz |
last post by:
Ugh....
All classes are copy by reference, even if you use "ByVal" and NOT "ByRef"
it's still a copy by reference.
Of course, as a consequence, if you change any values of the object you
passed...
|
by: xqxu.pzhou |
last post by:
I wrote a simple allocator "myAlloc" under the g++ 3.2.3. When it is
used by Vector, it works well. But when it is used by List, the codes
have errors when compling. the error message is:
"no...
|
by: jgscott |
last post by:
I've been trawling around for an answer to this question and thought
I'd try here. I have a class Graph, which has a std::list<Nodeas a
class member. Node it itself a class that makes extensive...
| |
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |