I need to use the built-in method 'reverse', but notice strange behavior.
Foe example:
print my_list.reverse() doesn't work.
new_list = my_list.reverse() doesn't work.
my_list.reverse()
print my_list does work. (displays reversed list)
But I want both a 'forward' and 'reverse' list:
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?
Thanks......Norm. 3 2043 en*******@ipns.com writes: print my_list.reverse() doesn't work.
The reverse() method reverses the list in place and returns None.
But I want both a 'forward' and 'reverse' list:
new_list = my_list # should save a 'forward' copy, right? Nope
No, both new_list and my_list are bound to the same list, like in C
you might have two pointers to the same structure. To make a copy, use
new_list = my_list[:]
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?
new_list = my_list[:]
new_list.reverse()
<en*******@ipns.com> wrote in message
news:3t********************************@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
>>> list1=[1,2,3] list2=list1[:] ## "cloning" list1.reverse()==None
1 list1
[3, 2, 1] list2
[1, 2, 3]
<en*******@ipns.com> wrote in message
news:3t********************************@4ax.com...
|
| I need to use the built-in method 'reverse', but notice strange behavior.
| Foe example:
|
| print my_list.reverse() doesn't work.
|
| new_list = my_list.reverse() doesn't work.
|
| my_list.reverse()
| print my_list does work. (displays reversed list)
|
| But I want both a 'forward' and 'reverse' list:
|
| 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?
|
| Thanks......Norm.
|
| This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Raymond Hettinger |
last post by:
Here is a discussion draft of a potential PEP.
The ideas grew out of the discussion on pep-284.
Comments are invited. Dart throwing is optional.
Raymond Hettinger
...
|
by: Raymond Hettinger |
last post by:
Please comment on the new PEP for reverse iteration methods.
Basically, the idea looks like this:
for i in xrange(10).iter_backwards(): # 9,8,7,6,5,4,3,2,1,0
<do something with i>
The...
|
by: Jim Langston |
last post by:
I have a class I designed that stores text chat in a
std::vector<sd::string>. This class has a few methods to retrieve these
strings to be displayed on the screen.
void ResetRead( bool Reverse,...
|
by: ford_desperado |
last post by:
Why isn't ALLOW REVERSE SCANS the default?
Why do we have to
- drop PK
- create an index
- recreate PK
What are the advantages of indexes that do not allow reverse scans?
|
by: Fady Anwar |
last post by:
Hi while browsing the net i noticed that there is sites publishing some
software that claim that it can decompile .net applications i didn't bleave
it in fact but after trying it i was surprised...
|
by: Frankie |
last post by:
Hello:
New user here...first post to group.
I'm getting an SQL syntax error when I try to run the following query:
$query = sprintf("SELECT itemNumber, entryDate, modifyDate, thumbnailURL,...
|
by: mike7411 |
last post by:
Is there any easy way to reverse the order of the bits in a byte in
C++?
(i.e. 00000001 becomes 10000000)
|
by: rick |
last post by:
Why can't Python have a reverse() function/method like Ruby?
Python:
x = 'a_string'
# Reverse the string
print x
Ruby:
x = 'a_string'
# Reverse the string
|
by: pbd22 |
last post by:
Hi.
I have a pesky gap to the right of each of my series of horizontal
tabs on the main nav bar of my site. I have tried a number of margin
and padding tricks (shown below) and can't seem to get...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |