A neat property of Python’s sort function is that it automatically sorts list of tuples by the first value of the tuple, this means that if you have a list, lets say of files and their sizes and you want to sort by filesize, you can do:
>>> a = [(200, 'foo.txt'), (100, 'bar.txt')] >>> a.sort() >>> a [(100, 'bar.txt'), (200, 'foo.txt')]
I just rediscovered this while reading Programming Collective Intelligence. I always find it oddly thrilling to run into these tidbits

Post a Comment