I want to get do the following (after annoyed by the methods in python which do not seem to return the original object):
take the values out of a dictionary d, and sort it in reverse order
values=d.values()
values.sort(reverse=True)
But is there a way to shorten it to one line? In general, I also want to do:
weight=range(1,27)
weight.reverse()
(I of course could use range(26,0,-1), but if I stick to what I wrote above, is there a way to do it like weight=range(1,27).reverse()?)
Usually, mutating methods in Python return
None.There are some built-ins useful to you in this particular case:
The
reversedandsortedbuiltins return new lists, so you can keep using the old one if you want.