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
#2
sort scores, you can pass key argument to sort().
(Jan-17-2021, 09:08 AM)banidjamali Wrote: I have successfully achieved the goal
also check the output of your code - it's incorrect, because you sort keys and values separately
banidjamali and ndc85430 like this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Just a comment: You sort the values but the name list is still untouched so the names and values in the result are no longer connected. You get the three highest values but the last three names. The output ought to be:
Top 1: Karen 92
Top 2: Josh 90
Top 3: Amin 81

which can be achieved by sorting the dictionary by value (or by item, which I think is the terminology here, but I'm far from sure). Use sorted and provide a key function (which has to be a lambda function as it will be "in-line"):
scores = {'Josh': 90, 'Adam': 72, 'Amin': 81, 'Narnia': 56, 'Natalie': 65, 'David': 68, 'Karen': 92, 'Elsa': 75}
scores = sorted(scores.items(), key=lambda x: x[1], reverse=True)
for i in range(3):
    print(f"Top {i+1}: {scores[i][0]} {scores[i][1]}")
banidjamali likes this post
Reply
#4
Another comment: The method given in my previous post gives a list of tuples. If you want an ordered dictionary (a contradiction as dictionaries are normally nor ordered) you may turn the list into a dictionary by simply:
scores = dict(sorted(scores.items(), key=lambda x: x[1], reverse=True))
also, if you need the original dictionary for other purposes in your program you might give the sorted dictionary another name like "sorted_scores" or something.
banidjamali likes this post
Reply
#5
(Jan-17-2021, 09:49 AM)Serafim Wrote: scores = sorted(scores.items(), key=lambda x: x[1], reverse=True)
Yes, I didn't notice the mistake in my output!
This line really helped.
Thank you all.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python program that calculat grades of students biligorm 3 2,192 Apr-04-2020, 10:11 AM
Last Post: buran
  python dictionary for students records atux_null 12 19,148 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