Ask coding questions

← Back to all posts
How do I create a leaderboard in python?
DarshanRajpara1 (13)

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 (6515) [earned 5 cycles]
View Answer
Commentshotnewtop
Coder100 (6515)

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!

DarshanRajpara1 (13)

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

Coder100 (6515)

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

Highwayman (1340)

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