Python Dictionary to sort a list in an alphabetical ascending order filter out only the student marks above 80

Viewed 1075

Input 1: {"Yash Pandya": 68, "Debaditya Basu": 89, "Shivam Gupta": 81, "Varun Goyal": 77, "Niranjan Gyancha": 64} Output 1: ['Debaditya Basu', 'Shivam Gupta']

Conditions: Given a dictionary containing the name and marks of some students, find out the students whose marks are at least 80. Return the student names in a list sorted in alphabetical ascending order.

# Read the input dictionary
import ast,sys

from collections import OrderedDict 
import operator

input_str = sys.stdin.read()
key_value = ast.literal_eval(input_str)

# function calling 
def dictionairy():  

# Declaring the hash function       
    key_value ={}     

# Keys and Values sorted in alphabetical order by the Values 
# sorted(key_value) returns an iterator over the  
# Dictionary’s value sorted in keys.  
# Note that it will sort in lexicographical order 
# For mathematical way, change it to float

final_result=sorted(key_value.items(), key = lambda kv:(kv[1], kv[0]))
#print(final_result)
res1=final_result[4][0]
res2=final_result[3][0]
#res3='['res1']'+'['res2']'
res3=res1+res2
print(res3)
#fin_list1= list(res1)
#fin_list2= list(res2)

# using + operator to concat 
#fin_list3 = fin_list1 + fin_list2 
#print(fin_list3)

# Printing concatenated list 
#print (str(fin_list3)) 

def main(): 
    # function calling 
    dictionairy() 

# main function calling 
if __name__=="__main__":      
    main()  

Actual Result:

Debaditya BasuShivam Gupta

Expected Result:

['Debaditya Basu', 'Shivam Gupta']

Where I want wron90g? Got 90% output. Assign me to get 10% ouput

1 Answers

You can do this in one line by using list comprehension.

sorted([i for i in your_dict if your_dict[i] > 80])

Related