In python,
how can i do something like that
a = ["pera", "uva", "maca", "saladamista"]
b = a.reverse()
but without assign the reverse list to a
?
In python,
how can i do something like that
a = ["pera", "uva", "maca", "saladamista"]
b = a.reverse()
but without assign the reverse list to a
?
First copy the list, then reverse the copy:
a = ["pera", "uva", "maca", "saladamista"]
b = a[:]
b.reverse()
or use the "Martian smiley":
b = a[::-1]
Edit: In case someone is interested in timings, here they are:
In [1]: a = range(100000)
In [2]: %timeit b = a[:]; b.reverse()
1000 loops, best of 3: 436 us per loop
In [3]: %timeit b = a[::-1]
1000 loops, best of 3: 414 us per loop
In [4]: %timeit b = list(reversed(a))
1000 loops, best of 3: 823 us per loop