Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Pass a list to diffrerent variables. (http://www.velocityreviews.com/forums/t330497-pass-a-list-to-diffrerent-variables.html)

user@domain.invalid 05-02-2004 12:56 PM

Pass a list to diffrerent variables.
 
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

Peter Otten 05-02-2004 01:10 PM

Re: Pass a list to diffrerent variables.
 
user@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

Robbie 05-02-2004 01:49 PM

Re: Pass a list to diffrerent variables.
 
Peter Otten wrote:

> user@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?

Peter Otten 05-02-2004 02:06 PM

Re: Pass a list to diffrerent variables.
 
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


Dominique Orban 05-02-2004 02:07 PM

Re: Pass a list to diffrerent variables.
 
On 2004-05-02, user@domain.invalid <user@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.

Jon Willeke 05-02-2004 02:08 PM

Re: Pass a list to diffrerent variables.
 
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.


All times are GMT. The time now is 10:06 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.