Ask coding questions

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

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

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

Hopes this helps!

Coder100 (18939)

no problem! Happy to help! @DarshanRajpara1

DarshanRajpara1 (15)

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

Coder100 (18939)

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

Highwayman (1505)

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