Pass a list to diffrerent variables.
Peter Otten
__peter__ at web.de
Sun May 2 09:10:14 EDT 2004
user at 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
More information about the Python-list
mailing list