When trying to pass the contents from one list to another this happens:
list = [1,2,3]
list1 = list
print list1
[1,2,3]
list.append(7)
print list1
[1,2,3,7]
Whats the easiest way to pass the data in a list, not the pointer, to
another variable 5 1306 us**@domain.invalid wrote: When trying to pass the contents from one list to another this happens:
list = [1,2,3]
Don't use list as a name. It hides the builtin list class.
list1 = list print list1 [1,2,3] list.append(7) print list1 [1,2,3,7]
Whats the easiest way to pass the data in a list, not the pointer, to another variable first = [1, 2, 3] second = list(first) # create a list from the sequence 'first' second.append(4) first
[1, 2, 3] third = first[:] # slice comprising all items of the 'first' list third.append(5) first
[1, 2, 3]
Both methods shown above result in a (shallow) copy of the original list.
Peter
Peter Otten wrote: us**@domain.invalid wrote:
When trying to pass the contents from one list to another this happens:
list = [1,2,3]
Don't use list as a name. It hides the builtin list class.
list1 = list print list1 [1,2,3] list.append(7) print list1 [1,2,3,7]
Whats the easiest way to pass the data in a list, not the pointer, to another variable
first = [1, 2, 3] second = list(first) # create a list from the sequence 'first' second.append(4) first [1, 2, 3] third = first[:] # slice comprising all items of the 'first' list third.append(5) first
[1, 2, 3]
Both methods shown above result in a (shallow) copy of the original list.
Peter
Thanks, that works fine but I am working with a 2d list...
and I dont understand why this happens
d = [[1,2,3],[1,2,3],[1,2,3],[1,2,3]]
d
[[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]
f = list(d)
f
[[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]
d[0][0]="a"
d
[['a', 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]
f
[['a', 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]
What exactly is this doing? And how can I stop it?
Robbie wrote: Thanks, that works fine but I am working with a 2d list... and I dont understand why this happens d = [[1,2,3],[1,2,3],[1,2,3],[1,2,3]] d [[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]] f = list(d) f [[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]] d[0][0]="a" d [['a', 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]] f [['a', 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]] What exactly is this doing? And how can I stop it?
While you have copied the outer list, both d and f share the same items, e.
g. d[0] and f[0] refer to the same item (a list containing 1,2,3 in this
case). That is called a "shallow" copy. To avoid such sharing, you need to
copy not only the outer list but also recursively the data it contains.
This is called a "deep" copy and can be done with the copy module: import copy a = [[1,2,3], [4,5]] b = copy.deepcopy(a) a[0][0] = "a" b[0][0]
1
A word of warning: I've never used this module in my code and think its
usage is a strong indication of a design error in your application. (Of
course I cannot be sure without knowing what you actually try to achieve.)
Peter
On 2004-05-02, us**@domain.invalid <us**@domain.invalid> wrote: When trying to pass the contents from one list to another this happens:
list = [1,2,3] list1 = list print list1 [1,2,3] list.append(7) print list1 [1,2,3,7]
Whats the easiest way to pass the data in a list, not the pointer, to another variable
Try list1 = list[:] instead. This creates a copy.
D.
Robbie wrote: Peter Otten wrote:
Both methods shown above result in a (shallow) copy of the original list.
Thanks, that works fine but I am working with a 2d list... and I dont understand why this happens d = [[1,2,3],[1,2,3],[1,2,3],[1,2,3]] d [[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]] f = list(d) f [[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]] d[0][0]="a" d [['a', 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]] f [['a', 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]] What exactly is this doing? And how can I stop it?
That's why Peter cautioned that the list() constructor yields a shallow
copy. The is operator and the id() function will reveal that d[0][0]
and f[0][0] are the same list. You want the copy.deepcopy() function. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Doug Jordan |
last post by:
I am fairly new to Python. This should be an easy answer but I cannot get
this to work. The code is listed below. I know how to do this in C,
Fortran, and VB but it doesn't seem to work the same...
|
by: nicver |
last post by:
I am fixing a client's Web site and for some reason an ASP class does
not want to use the variables it retrieves when it initialiases.
This is an excerpt of the class and it is enough to show...
|
by: dk |
last post by:
Hi all,
Would appreciate some advice on the following:
I am trying to speed up an Access database connected to a SQL Server
back-end. I know I can use a pass-through query to pass the sql...
|
by: macyp |
last post by:
I have to pass values from one aspx page to another. The controls I
have in the first page are:
a textbox, 3 drop down lists, and 2 check boxes, and a submit button.
It is a search page, and the...
|
by: tjjones70 |
last post by:
I am trying to load list boxes from an Access database. I'm looking at 3 tables and for each table I'm loading a list box that has current information and another list box that has expired...
|
by: John Dalberg |
last post by:
I have seen examples for List<T>.FindAll(findthis)where findthis is a
predicate. How do I pass a parameter to this predicate so that I have
different values to search for? I don't want to use...
|
by: lisp9000 |
last post by:
I've read that C allows two ways to pass information between
functions:
o Pass by Value
o Pass by Reference
I was talking to some C programmers and they told me there is no such
thing as...
|
by: raylopez99 |
last post by:
Keywords: scope resolution, passing classes between parent and child
forms, parameter constructor method, normal constructor, default
constructor, forward reference, sharing classes between forms....
|
by: John O'Hagan |
last post by:
Hi Pythonistas,
I'm looking for the best way to pass an arbitrary number and type of variables
created by one function to another. They can't be global because they may
have different values...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
| |