I'm trying to sort my simple game's leaderboard and print out the top 5 scores, but it just prints out the whole list in the order it was saved as and not organised after adding
for eachline in y[:5]:
print(eachline)
, as without this code, it prints the entire list in numerical order from smallest to highest, which isn't what I need. Any help would be much appreciated. Leaderboard file link: https://drive.google.com/open?id=1w51cgXmmsa1NWMC-F67DMgVi3FgdAdYV
leaders = list()
filename = 'leaderboard.txt'
with open(filename) as fin:
for line in fin:
leaders.append(line.strip())
y = sorted(leaders, key = lambda x: float(x[0]))
for eachline in y[:5]: #after this and the next line it prints just the file contents
print(eachline)