-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdict_example.py
More file actions
22 lines (13 loc) · 917 Bytes
/
dict_example.py
File metadata and controls
22 lines (13 loc) · 917 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#Program to calculate the rank of the students
data = {} #Creating an empty dictionary
n = int(input("Enter no. of Students : "))
for i in range(n):
name=input(f'\nStudent {i+1} : ') #input keys (names of the students)
marks=int(input(f"{name}'s total marks : ")) #input values (marks of the student)
data[f"{name}"]=marks #updating the dictionary
sorted_dict=dict(sorted(data.items(),reverse=True,key=lambda item:item[1])) #Sorting the dictionary by marks(i.e, by Values),"key" parameter is passed to perform a custom sort that's why index 1 is used in lambda function for values as index 0 is reserved for keys.
sorted_list=list(sorted_dict.keys())
print(f"\nCongratulations!! {sorted_list[0]} for being the 1st rank holder.\n")
print("Other's rank are as follow : \t")
for i in range(len(sorted_list)):
print(f"\tRank {i+1} : {sorted_list[i]}") #printing the list w.r.t their index value