Tuesday, August 22, 2017

Sorting a List of Tuples by Second Element

For a project that I am working on I needed to be able to sort a list of scores and initials. This is what I came up with:


# Sorting a list of tuples by second element in tuple.

scores = sorted([('plyr1', 350), ('plyr2', 425), ('plyr3', 1250), ('plyr4', 500)], key = lambda x: x[1], reverse = True)

print (scores)