Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Top 3 students
#1
Hello everyone. Hope you are all having a great time.

Here's a simple question:

There's a dictionary, and I want to extract the top 3 students from it.
I have successfully achieved the goal and the code is pretty much readable (I suppose), but I'm sure there's a more concise way of achieving the same output (using less lines of code).

scores = {'Josh': 90, 'Adam': 72, 'Amin': 81, 'Narnia': 56, 'Natalie': 65, 'David': 68, 'Karen': 92, 'Elsa': 75}

names = list(scores.keys())
values = list(scores.values())
values.sort()

x = 1
for i in values:
    while x < 4:
        print(f"Top {x}:", names[-x] ,values[-x])
        x += 1
Output:
Top 1: Elsa 92 Top 2: Karen 90 Top 3: David 81
Thank you all in advance.
Reply


Messages In This Thread
Top 3 students - by banidjamali - Jan-17-2021, 09:08 AM
RE: Top 3 students - by buran - Jan-17-2021, 09:30 AM
RE: Top 3 students - by Serafim - Jan-17-2021, 09:49 AM
RE: Top 3 students - by Serafim - Jan-17-2021, 10:04 AM
RE: Top 3 students - by banidjamali - Jan-17-2021, 11:59 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  python program that calculat grades of students biligorm 3 2,272 Apr-04-2020, 10:11 AM
Last Post: buran
  python dictionary for students records atux_null 12 19,316 Nov-12-2017, 04:47 PM
Last Post: atux_null

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020