#### Exrcise 102

We consider the following dictionary whose keys are the names of the students and the key values are the general averages obtained by passing the final exam:

`students = {"student_1" : 13 , "student_2" : 17 , "student_3" : 9 , "student_4" : 15 , `

"student_5" : 8 , "student_6" : 14 , "student_7" : 16 , "student_8" : 12 ,

"student_9" : 13 , "student_10" : 15 , "student_11" : 14 , "student_112" : 9 ,

"student_13" : 10 , "student_14" : 12 , "student_15" : 13 , "student_16" : 7 ,

"student_17" : 12 , "student_18" : 15 , "student_19" : 9 , "student_20" : 17 ,}

Write a Python program that partitions this dictionary into two sub-dictionaries:

- admittedStudents whose keys are the admitted students and the key values are the averages obtained (average greater than or equal to 10).
- nonAdmittedStudents whose keys are non-admitted students and the key values are the averages obtained (average less than or equal to 10).

#### Solution

`students = {"student_1" : 13 , "student_2" : 17 , "student_3" : 9 , "student_4" : 15 , `

"student_5" : 8 , "student_6" : 14 , "student_7" : 16 , "student_8" : 12 ,

"student_9" : 13 , "student_10" : 15 , "student_11" : 14 , "student_112" : 9 ,

"student_13" : 10 , "student_14" : 12 , "student_15" : 13 , "student_16" : 7 ,

"student_17" : 12 , "student_18" : 15 , "student_19" : 9 , "student_20" : 17 ,}

# we create two empty dictionaries, one for the admitted and the other for the non-admitted students

admittedStudents = dict ({})

nonAdmittedStudent = dict ({})

# we browse the list of keys and values simultaneously:

# if the key is < 10 the student will be added to the dictionary of students not admitted

# if not, the student will be added to the dictionary of admitted students

for key, value in students.items ():

if (value <10):

nonAdmittedStudent[key] = value

else:

admittedStudents[key] = value

print ("Admitted students:", admittedStudents)

print ("Students not admitted:", nonAdmittedStudent)

# The output is:

#Admitted students: {'student_1': 13, 'student_2': 17, 'student_4': 15, 'student_6': 14, 'student_7': 16, 'student_8': 12, 'student_9': 13, 'student_10': 15, 'student_11': 14, 'student_13': 10, 'student_14': 12, 'student_15': 13, 'student_17': 12, 'student_18': 15, 'student_20': 17}

#Students not admitted: {'student_3': 9, 'student_5': 8, 'student_112': 9, 'student_16': 7, 'student_19': 9}

**Younes Derfoufi**

my-courses.net

my-courses.net

I did this using Dictionary comprehensions:

admittedStudents={dic:students[dic] for dic in students if students[dic]>=10 }