0

Dictionary reference error

#Write a function called students_present. students_present #should take as input one parameter, a dictionary. The keys #of the dictionary will be names, and the values will be one #of three strings: "Here", "Present", or an empty string "". # #Return a list of the keys for whom the corresponding value #is either "Here" or "Present". #Add your code here! result = [] def students_present(list): for key in list.keys(): value = list[key] if value != "": result.append(key) return result student_list = {"David" : "Here", "Marguerite" : "Here", "Jackie": "", "Joshua": "Present", "Erica": "Here", "Daniel": ""} print(students_present(student_list)) #If your function works correctly, this will originally #print (although the order of the keys may vary): #["David", "Marguerite", "Joshua", "Erica"] But my code is printing the result twice & I dont understand why.. Please help. ["David", "Marguerite", "Joshua", "Erica", "David", "Marguerite", "Joshua", "Erica"].

9th Oct 2020, 8:22 PM
NG_
NG_ - avatar
2 Antworten
+ 5
# I would advise to simplify: d = {"David": "Here", "Marguerite": "Here", "Jackie": "", "Joshua": "Present", "Erica": "Here", "Daniel": ""} print([*filter(d.get, d)])
9th Oct 2020, 10:44 PM
Vitaly Sokol
Vitaly Sokol - avatar
+ 1
Your code works lol. It doesn't print it twice for me. Maybe you called the function twice or something.
9th Oct 2020, 8:34 PM
Zerokles
Zerokles - avatar