I'm trying to use the reverse() method in the following way:
>>> L=[1,2,3]
>>> R=L
>>> L.reverse()
>>> L
[3, 2, 1]
>>> R
[3, 2, 1]
Why does it reverse R as well? How do I keep the original list and create a reversed on?
Thanks!
I'm trying to use the reverse() method in the following way:
>>> L=[1,2,3]
>>> R=L
>>> L.reverse()
>>> L
[3, 2, 1]
>>> R
[3, 2, 1]
Why does it reverse R as well? How do I keep the original list and create a reversed on?
Thanks!
you need to make a copy of your list
L=[1,2,3]
# R=L
R = L[:]
L.reverse()
or more directly (reversing using slice notation):
R = L[::-1]
if you just write R = L
then R
is just a new reference on the same list L
.
if you also need to copy the elements in your list, use copy.deepcopy
; R = L[:]
only produces a shallow copy (which is fine in your case where there are only int
s in the list).
To avoid reversing R
, you need a copy of L
To make a copy, change
R=L <---- this is not copying L in to R
to
R= L[:] <---- this will create a copy of L in to R
The operation
R=L
means that the variable R stores whatever L is storing i.e R references to whatever value L is refering. Now, when you reverse L, it means the list L is refering to is reversed.As R is also refereing same address, you are seeing R as reversed.
You have been bitten by the fact that Python assigns references, not copies.
Python uses reference exclusively in order to prevent unneeded copies. It uses references also when passing function arguments, and even with default function arguments. It only makes a copy when you explicitly tell it to, e.g. by doing
L = list(R)
or
from copy import copy
L = copy(R)
The difference between the two is that list(R)
always returns a list, where copy
returns an object of the same type as the original. Also, copy
does not work with iterators, but list
does.
Note that these are 'shallow' copies. If the list contains objects, the new list contains references to the same objects. For 'deep' copies, it has the function deepcopy
in the module copy
.
from copy import deepcopy
L = deepcopy(R)
But in practise there is rarely a need to make explicit copies. Python provides e.g. the reversed
built-in that can be used to iterate through the list.
for l in reversed(L):
# do stuff
It returns an iterator, so it is efficient. No need to duplicate the list. Other cases where a copy might be needed can be handled by e.g. generator functions. A valid case for a copy might be when pasing data past thread boundaries.