Pesky reverse()

Sean Ross sross at connectmail.carleton.ca
Thu Jan 8 21:33:22 EST 2004


<engsolnom at ipns.com> wrote in message
news:3t3svvs7ni8l8jnn1l1v60oogirqoaa85f at 4ax.com...
> I need to use the built-in method 'reverse', but notice strange behavior.

[snip]

> new_list = my+list  # should save a 'forward' copy, right? Nope
> my_list.reverse() actually reverses both copies, since Python is a bit too
> helpful sometimes, and I understand why.
>
> So the question is, how do I get a forward and reverse list?

>>> forward = range(10)
>>> reverse = forward[:]   # copy
>>> reverse.reverse()
>>> print forward
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> print reverse
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
>>>

HTH
Sean





More information about the Python-list mailing list