sorting the list by inner elements

Stefan Axelsson crap1234 at hotmail.com
Wed Mar 24 10:34:36 EST 2004


John Hunter wrote:
> The usual way is to simply reverse the tuple, sort, and then reverse
> back
> 
>   tmp = [ (city, name) for name, city in name_city_list]
>   tmp.sort()
>   name_city_list = [ (name, city) for city, name in tmp]
> 
> This is usually faster than writing a custom comparison func for sort.

The notable exception being when Psyco is applied. I've actually had 
e.g. significantly *faster* runs when applying a custom sort function to 
do a 'reverse' than calling it separately after sorting. Do a run on 
your own data/platform to check what's faster if speed matters. If it 
doesn't I'd do the comparison function as I find that clearer.

Stefan,
-- 
Stefan Axelsson  (email at http://www.cs.chalmers.se/~sax)



More information about the Python-list mailing list