Sort list by column preserving index. 

This is yet another reason I love Python! 

This is good for something like generating HTML tables with rowspans that you want sorted. Each tuple is a row, each item in the tuple is a cell. If you sort this data it will change the index so if you set a rowspan on the first row and sort, it will likely span on the wrong row. This resolves that problem.

# Sort by column, in this case the last column, and keep original list index.
rows = [('Fred', 'Flintstone', 46, 0),('Wilma', 'Flintstone', 45, 33),('Pebbles', 'Flintstone', 10, 55)]
for index,row in sorted(enumerate(rows), key=lambda item: item[-1], reverse=True):
    print (index, row)

~ by mohawke on September 11, 2015.