简体   繁体   中英

How to return a reversed list back to its original state in python

I was just curious as to whether or not there is a function to turn a reversed list back to its original form or would the solution just to be to write the code to return it myself?

I do understand that i can simply assign the list to a variable(x) prior to reversing and then just assign the list back to x, but i am just curious.

I did think that calling the reverse() function twice would do this, but it did not,

Any answers are appreciated.

my_list = ["This", "is", "an", "example", "list"]
x = my_list
my_list.reverse()
print(my_list)
my_list = x
print(my_list)

usereversed() instead (which would return a new reversed iterable instead of reversing the list in place)

my_list = ["This", "is", "an", "example", "list"]
x = list(reversed(my_list))
print(x)
print(my_list)

output:

['list', 'example', 'an', 'is', 'This']
['This', 'is', 'an', 'example', 'list']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM