Skip to content
← Back to Community
How do I create a leaderboard in python?
Profile icon
DarshanRajpara1

I am trying to create a leaderboard for python and cannot figure it out. I want the leaderboard to be sortable and in this format:

Score | Name

Can anyone help me?

Answered by Coder100 [earned 5 cycles]
View Answer
Voters
Profile icon
tsanu2007
Profile icon
MontePayne
Profile icon
Leroy01010
Profile icon
Highwayman
Profile icon
DarshanRajpara1
Comments
hotnewtop
Profile icon
Coder100

Hello. You can sort an array, then map the array, and finally join the array.

The code is going to be something like this:

users = [("username", 5), ("someone", 15), ('n00b', 0)] # Sort user array users.sort(key=lambda a: a[1], reverse=True) # Format the users leaderboard = map(lambda user: user[0] + "| " + str(user[1]), users) # Output print("\n".join(leaderboard))

Hopes this helps!

Profile icon
DarshanRajpara1

@Coder100 Thank you!

Profile icon
Coder100

no problem! Happy to help! @DarshanRajpara1

Profile icon
DarshanRajpara1

@Coder100 Just one question:
What if I wanted it to save it to a file?

Profile icon
Coder100

you can use file i/o lemme find it for ya @DarshanRajpara1

Profile icon
Highwayman

Hm... what exactly are you having trouble with tho? Is it the sorting part? The saving part?