Pass a list to diffrerent variables.

U

user

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
 
P

Peter Otten

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
 
R

Robbie

Peter said:
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?
 
P

Peter Otten

Robbie said:
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
 
D

Dominique Orban

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

Jon Willeke

Robbie said:
Peter said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top