Mar-12-2017, 10:45 AM
Little necronitpicking:
sort with reverse keyword gives a list ordered in a reverse order, not a reversed list. For the number/list used as an example it does not matter, as it was ordered, but otherwise it could. Beside [::-1] it could be done with
As "reversing" list is equivalent to sorting it on index in decreasing order, I tried to abuse sort/sorted to get such result and ended with:
sort with reverse keyword gives a list ordered in a reverse order, not a reversed list. For the number/list used as an example it does not matter, as it was ordered, but otherwise it could. Beside [::-1] it could be done with
.reverse()
or reversed()
.As "reversing" list is equivalent to sorting it on index in decreasing order, I tried to abuse sort/sorted to get such result and ended with:
In [38]: a = [1, 2, 5, 3] In [39]: sorted(a, key = lambda x, i=iter(range(len(a))) : -next(i)) Out[39]: [3, 5, 2, 1]it looks ugly, but probably less ugly than:
In [41]: list(map(lambda x:x[1], sorted(enumerate(a), reverse=True))) Out[41]: [3, 5, 2, 1]Perhaps there are other ways how to reverse list with sort/sorted one-liner and built-ins ...