Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Pass a list to diffrerent variables.

Reply
Thread Tools

Pass a list to diffrerent variables.

 
 
user@domain.invalid
Guest
Posts: n/a
 
      05-02-2004
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
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      05-02-2004
http://www.velocityreviews.com/forums/(E-Mail Removed)lid 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
 
Reply With Quote
 
 
 
 
Robbie
Guest
Posts: n/a
 
      05-02-2004
Peter Otten wrote:

> (E-Mail Removed)lid 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?
 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      05-02-2004
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

 
Reply With Quote
 
Dominique Orban
Guest
Posts: n/a
 
      05-02-2004
On 2004-05-02, (E-Mail Removed)lid <(E-Mail Removed)> 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.
 
Reply With Quote
 
Jon Willeke
Guest
Posts: n/a
 
      05-02-2004
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Diffrerent output of the same program on HP aCC and Linux (Intel/g++) Alex Vinokur C++ 2 12-19-2012 06:31 PM
Different C-preprocessors have diffrerent behavior Alex Vinokur C Programming 1 07-15-2012 11:03 AM
difference between pass by address and pass by reference!! blufox C Programming 2 04-03-2006 02:53 PM
Pass by reference / pass by value Jerry Java 20 09-09-2005 06:08 PM
How to pass variable argument list to another function w/ variable argument list? Ben Kial C Programming 1 11-15-2004 01:51 AM



Advertisments