By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,172 Members | 762 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,172 IT Pros & Developers. It's quick & easy.

Pass a list to diffrerent variables.

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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
Jul 18 '05 #2

P: n/a
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?
Jul 18 '05 #3

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

Jul 18 '05 #4

P: n/a
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.
Jul 18 '05 #5

P: n/a
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.
Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.